how to apply check mark on table view in iphone using objective c?

后端 未结 2 800
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-14 00:02

i m trying for apply for check mark in table view, but it is not working, if i checked again at that cell again then check mark apply. but not apply at new selected cell. a

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-14 00:28

    There is a funny bug here

     if (row != oldRow)
    

    because if oldRow was nil (or in another words... "0" ;-) and the user press the row = "0" the "if" sentence will think this row was checked.

    I fixed like this:

    - (void) tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath
    {
        if (!self.lastIndexPath) {
            self.lastIndexPath = indexPath;
        }
    
        if ([self.lastIndexPath row] != [indexPath row])
        {
            UITableViewCell *newCell = [tableView cellForRowAtIndexPath: indexPath];
            newCell.accessoryType = UITableViewCellAccessoryCheckmark;
    
            UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.lastIndexPath]; 
            oldCell.accessoryType = UITableViewCellAccessoryNone;
    
            self.lastIndexPath = indexPath;  
        }
        else {
            UITableViewCell *newCell = [tableView cellForRowAtIndexPath: indexPath];
            newCell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    

    By the way... dont forget to use "self"

提交回复
热议问题