iOS-单选cell的实现

一、思路

  先设置一个chooseCelltag标记(类型为NSIndexPath),然后在点击cell触发的时候,如果tag设置有值,就设置

     

 UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:tag];

          selectedCell.accessoryType = UITableViewCellAccessoryNone;

 

  然后把tag = indexPath

  然后

  

cell.accessoryType = UITableViewCellAccessoryCheckmark;

 

二、主要代码:

1、在.m的interface中

@interface ChooseTypeViewController ()

{

    NSIndexPath *tag; 

}

@end

 

2、在点击cell触发的事件中。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    [tableView deselectRowAtIndexPath:indexPath animated:TRUE];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath: indexPath];

    if (tag) {

        UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:tag];

        selectedCell.accessoryType = UITableViewCellAccessoryNone;

    }

    tag = indexPath;

    cell.accessoryType = UITableViewCellAccessoryCheckmark;

}

 

posted @ 2015-02-09 09:37  zhanggui  阅读(2004)  评论(0编辑  收藏  举报