Okay, so my issue is that I’m deleting rows in a tableview upon a click on a button, and I’m identifying the row to delete by getting the sender’s tag and removing it from my data source array and deleting the row from the table…
In cellForRowAtIndexPath (relevant part is deleteBtn): *BELOW IS THE FIXED AND WORKING CODE
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"questionCell";
HTQuestionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell == nil)
{
cell = [[HTQuestionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *dict = [NSDictionary dictionary];
dict = _questionsArray[indexPath.row];
NSMutableAttributedString *atbString;
NSMutableAttributedString *atbQuestionString;
atbString = [[NSMutableAttributedString alloc] initWithString:[dict objectForKey:@"person"] attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:34]}];
atbQuestionString = [[NSMutableAttributedString alloc] initWithString:[dict objectForKey:@"question"] attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:34]}];
[atbString appendAttributedString:atbQuestionString];
[cell.questionTxtView setAttributedText:atbString];
[cell.deleteBtn setTag:indexPath.row];
[cell.showBtn setTag:indexPath.row];
NSLog(@"Delete Button : %@", cell.deleteBtn);
[cell.deleteBtn addTarget:self action:@selector(deleteMsg:) forControlEvents:UIControlEventTouchUpInside];
[cell.showBtn addTarget:self action:@selector(showMsg:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
And in the delete method I then do this:
UIButton *tempButton = sender;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:tempButton.tag inSection:0];
[_questionsArray removeObjectAtIndex:tempButton.tag];
[_dataTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[_dataTable reloadData];
I’ve also tried with beginUpdates and endUpdates instead of reloadData, but none have any effect…
So, when I’m testing it, I have two rows… Initially their tag’s get set correctly – 0 and 1… But when I then delete row 0, and check the tag for the new first row, it still has the tag = 1, when it now should be 0… It’s almost like it doesn’t update the button’s tag after the deletion…
Solution 1: use:
to reset the tags, as cellForRowAtIndexPath is called again
Solution 2: use:
which will also call cellForRowAtIndexPath and update the tags