Question

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:

- (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;

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

Good luck! :)

Was it helpful?

Solution

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:

cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [tableSubtitles objectAtIndex:indexPath.row];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top