My problem is the following: I assign a mutable array to the cellForRowAtIndexPath so that it displays each array object in a cell. So far so good, the cells are displayed as expected. Now i want a display (according to a condition) a UILabel in the first cell, so that the other mutable array objects will be shifted to the second cell, and third, etc.
The issue is that, when i test that condition, and it’s true, the UILabel is displayed in the first cell with the first object. So actually, two elements are in the same cell, which is not what i expect. I want (when the condition is true) to shift all the elements, so that they will be displayed from the second cell in order to leave the first cell to the UIlabel.
My relevant code which doesn’t give me what i expect, is this:
- (UITableViewCell*)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"any-cell"];
// Add and display the Cell
cell.tag = [indexPath row];
NSLog(@"cell.tag= %i",cell.tag);
//test the condition, if it's ok, then add the label to the first cell
if ([self isNoScoreLabelDisplayed] && cell.tag==0) {
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 220, 50)];
[lbl setBackgroundColor:[UIColor greenColor]];
[cell addSubview:lbl];
}
cell.tag = [self isNoScoreLabelDisplayed]?[indexPath row]+1:[indexPath row];//here i wanted to shift the tags in case the condition is true, so that all the elements will be displayed from the second cell. But seems not doing what i want :(
//
if (indexPath.row < cellList.count) {
[cell addSubview:[cellList objectAtIndex:[indexPath row]]];//cellList is the mutable array from which i get all the elements to display in the cells
}else{
[cell addSubview:nextButton];
}
return cell;
}
Am i missing something in my logic? thanx in advance.
You can insert your label’s text as a first element in your container and check for it. This way you will save yourself any need for index offsets and further complications of your code.
E.g.