2017-07-12 19 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