2017-07-12 17 views
-1

我有以下实现,用户可以根据需要选择尽可能多的项目。tableView上的对号标记

一旦用户选择该项目,我可以看到复选标记,但是当用户向上或向下滚动时,会出现奇怪的行为,因为我看到其他项目也被选中,即使未选中也是如此。

例如,如果我按顺序选择3个项目,当向下滚动时,我可以看到选定的相同图案(按顺序3个项目)。这是否与dequeueReusableCellWithIdentifier有关?如果是,我做错了什么?

- (void)viewDidLoad { 

    [super viewDidLoad]; 
    self.comboTableView.allowsMultipleSelection = YES; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [comboTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 
} 
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [comboTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; 
} 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return movies.count; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return ((Section*)movies[section]).movies.count ; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    cell.textLabel.text = ((Section*)movies[indexPath.section]).movies[indexPath.row]; 
    return cell; 
} 
+0

复制https://stackoverflow.com/questions/23727255/multiple-checkmark-when-row-selected-in-uitableview-ios – Bejibun

回答

2

UITableView Cells是因为你使用dequeueReusableCellWithIdentifier:cellIdentifier重用。所以你必须检查当前的索引路径项是否被选中。如果选中,则启用刻度标记。否则禁用它

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    NSArray <NSIndexPath*> *selectedIndexPaths = [tableView indexPathsForSelectedRows]; 

    if ([selectedIndexPaths containsObject:indexPath]) { 
      // Add the code of enable tick mark 
    } else { 
      // Add the code of disable tick mark 
    } 
} 
1

这个问题是因为你'检查tableviewcell'而不是你的数据。 所以如果你没有记录这个检查信息,你的tableviewcell会记录检查状态,即使这个单元格实例改变为呈现不同indexPath的不同数据。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [comboTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 
    ((Section*)movies[indexPath.section]).movies[indexPath.row].checked = true; 
} 
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [comboTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; 
    ((Section*)movies[indexPath.section]).movies[indexPath.row].checked = false; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    cell.textLabel.text = ((Section*)movies[indexPath.section]).movies[indexPath.row]; 
    cell.accessoryType = ((Section*)movies[indexPath.section]).movies[indexPath.row].checked ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; 
    return cell; 
} 
+0

我真的跟踪每个选择在我的数据通过在我的模型类中添加另一个变量,或者只是让tableview为我处理它?我已经高举你的努力 – hotspring

+0

是的,你应该记录,因为tableview不会为你处理这个。这就是你困惑的原因。 – Codus

+0

'[tableView indexPathsForSelectedRows]'存储选定的'section'和'row'(indexPath),不是吗? – hotspring