Xcode tableview 副标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10632954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:21:22  来源:igfitidea点击:

Xcode tableview subtitle

xcodetableviewcellsubtitle

提问by Mikkel V.

I have been searching for a while trying to get help, but without luck :(

我一直在寻找一段时间试图寻求帮助,但没有运气:(

I want to set the subtitle in TableViewCells. The subtitles must not be the same in every cell. I have been writing some code:

我想在 TableViewCells 中设置副标题。每个单元格中的字幕不得相同。我一直在写一些代码:

- (void)viewDidLoad
{
[super viewDidLoad];
 tableData = [[NSArray alloc] initWithObjects:@"cell 1", @"cell 2", nil];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
return [tableData count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath        *)indexPath 
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
if (cell == nil)
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle      reuseIdentifier:@"MyCell"];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row]; 

cell.detailTextLabel.text = @"Subtitle 1", @"Subtitle 2", nil;

return cell;
}

The problem is in cell.detailTextLabel.text = @"Subtitle 1", @"Subtitle 2", nil;

问题出在 cell.detailTextLabel.text = @"Subtitle 1", @"Subtitle 2", nil;

Does anybody know how to set a different subtitle in each cell?

有人知道如何在每个单元格中设置不同的字幕吗?

Good luck! :)

祝你好运!:)

回答by Alladinian

Exactly the same way you're setting the cell titles. You can have another array holding the subtitles and get them like this:

与设置单元格标题的方式完全相同。您可以拥有另一个包含字幕的数组,并像这样获取它们:

- (void)viewDidLoad
{
    [super viewDidLoad];
    tableData = [[NSArray alloc] initWithObjects:@"cell 1", @"cell 2", nil];
    // Assuming that you have declared another property named 'tableSubtitles'
    tableSubtitles = [[NSArray alloc] initWithObjects:@"sub1", @"sub2", nil];
}

Then in your tableView:cellForRowAtIndexPath:method:

然后在你的tableView:cellForRowAtIndexPath:方法中:

cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [tableSubtitles objectAtIndex:indexPath.row];