In my uitableview I have two sections
first is fetched from core data
other is added through textfield which is stored in NSMutableArray(otherFriends)
here is my code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if([otherFriends count]>0)
{
return 2;
}
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
if(otherFriends == 0)
{
return [[[[self fetchedResultsController]sections]objectAtIndex:0]numberOfObjects];
}
return [otherFriends count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"newGroupCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Light" size:20.0];
cell.textLabel.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.0f];
if(indexPath.section == 0)
{
user = [[self fetchedResultsController] objectAtIndexPath:indexPath];
cell.textLabel.text = user.name;
cell.detailTextLabel.text = user.primaryResource.status;
[cell.imageView setFrame:CGRectMake(0, 0, 50, 50)];
[cell.imageView.layer setMasksToBounds:YES];
if (user.photo != nil)
{
cell.imageView.image = user.photo;
}
else
{
cell.imageView.image = [UIImage imageNamed:@"defaultPerson"];
}
}
else
{
cell.textLabel.text = [otherFriends objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"defaultPerson"];
}
return cell;
}
First section also have subtitles but second section cells have no subtitles
when I am adding new friend it works fine till all rows are visible, but when adding new friend and that row is not visible and to view that row I must scroll, then this row show the subtitle of first row in section 0(the very first cell) and its repeating in section 1 third row. Only subtitle is repeating but the main text is not.
For several hours I am trying to figure out this but no luck.
This is because in your
elsebranch you are not setting thecell.detailTextLabel.text.When a cell gets recycled, the old detailTextLabel stays there. You need to set all properties of the cell in both branches of the conditionsl, on the chance that it has been recycled.