TableView 校检表

这俩天学习了tableView 校检表 主要就是通过一个方法来跟踪当前选中的行。下面将声明一个NSIndexPath 的属性来跟踪最后选中的行。这篇文章希望能给那些初学者带来学习的乐趣。不说了直接上代码。

         

              首先在AppDelegate.m中声明一个控制器 navController基于TableView

 

     BIDFirstLevelController *first = [[BIDFirstLevelController alloc]
                                      initWithStyle:UITableViewStylePlain];
    self.navController = [[UINavigationController alloc]
                          initWithRootViewController:first];
    [self.window addSubview:navController.view];

        

然后创建新文件基于UITableViewController 的 TableView 的文件。在.h声明两个属性list 和*lastIndexPath;Path

@property (strong, nonatomic) NSArray *list;

@property (strong, nonatomic) NSIndexPath *lastIndexPath;


别忘了添加TbaleView 的协议。然后就是在.m里面去实现各种方法

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *array = [[NSArray alloc] initWithObjects:@"Who Hash",
                      @"Bubba Gump Shrimp Étouffée", @"Who Pudding", @"Scooby Snacks",
                      @"Everlasting Gobstopper", @"Green Eggs and Ham", @"Soylent Green",
                      @"Hard Tack", @"Lembas Bread", @"Roast Beast", @"Blancmange", nil];
    self.list = array;
}

- (void)viewDidUnload {
    [super viewDidUnload];
    self.list = nil;
    self.lastIndexPath = nil;
}

#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section {
    return [list count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CheckMarkCellIdentifier = @"CheckMarkCellIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             CheckMarkCellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:CheckMarkCellIdentifier];
    }
    NSUInteger row = [indexPath row];//从这个单元和当前选项中提取行;
    NSUInteger oldRow = [lastIndexPath row];
    cell.textLabel.text = [list objectAtIndex:row];//从数组中获得这一行的值,并将它分配给单元的标题
    cell.accessoryType = (row == oldRow && lastIndexPath != nil) ?
    UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    然后,根据两行是否相同,将扩展图标设置为显示检验标记或者不显示任何东西。换句话说如果某行的表正在请求单元,而这行正好是当前选中的行,我们就将就扩展图标设置为一个选中标记;否则他就不显示任何东西。注意,还需要检查lastIndexPath来确定它不为nil  。这样做是因为值为nil 的lastIndex 表示没有任何选项。但是在nil对象上调用row的方法将返回0 他是一个有效行,不过我们不希望在0行上放置一个检验标记。因为实际上,没有任何选项。
    return cell;
}

#pragma mark -
#pragma mark Table Delegate Methods
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    int newRow = [indexPath row];
    int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
    //新旧两行如果相同就不做更改
    if (newRow != oldRow) {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
                                    indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
        
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:
                                    lastIndexPath];
        oldCell.accessoryType = UITableViewCellAccessoryNone;
        
        self.lastIndexPath = indexPath;//存储刚才在lastIndex中选中的索引路线,以便再下一次选中一行时使用:
    }
//完成之后,告知表示图取消选中刚才的行,因为我们不希望该行一直保持突出显示。我们已经选中标记了改行,把他保留为蓝色将是很麻烦的一件事。
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

下一步,获取刚才选中的单元,并制定一个校检标记作为它的扩展图标,然后获取上一次选中的单元,将他的扩展图标设置为无

原文地址:https://www.cnblogs.com/riskyer/p/3343399.html