2014-04-20 69 views
0

我有一個奇怪的uitableview選擇問題。試圖在預先選擇一對單元格的情況下使用多個選擇。多項選擇設置爲true。UITableView選擇 - 多選

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; 
    cell.textLabel.font = [UIFont systemFontOfSize: 15.0f]; 
    cell.textLabel.text = [data objectAtIndex:indexPath.row]; 

    // if([_selectedInts containsObject:[NSNumber numberWithInt:(int)indexPath.row]]){ 
    // 
    //  [cell setSelected:YES animated:NO]; 
    // 
    // } else { 
    // 
    //  [cell setSelected:NO animated:NO]; 
    // 
    // } 


    return cell; 
} 



-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ 

    cell.selectionStyle = UITableViewCellSelectionStyleGray; 
    [cell setUserInteractionEnabled:YES]; 


    if([_selectedInts containsObject:[NSNumber numberWithInt:(int)indexPath.row]]){ 

     [cell setSelected:YES animated:YES]; 
     [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; 
    } else { 

     [cell setSelected:NO animated:YES]; 
     [cell setAccessoryType:UITableViewCellAccessoryNone]; 
    } 

} 

當tableview加載一堆單元格時,由於標記需要選擇的索引的整數列表而被選中。

這些單元格停止響應,不會在獲取tapped時發出didselect和diddeselect方法。

什麼問題?

回答

1

我不知道爲什麼投入willDisplayCell原因代碼這樣的事情發生,但我發現當我嘗試你的代碼時也是如此。將代碼放在cellForRowAtIndexPath中:如果我使用selectRowAtIndexPath:animated:scrollPosition:而不是setSelected:(單元格沒有顯示選擇顏色,如果我使用setSelected :,但它們是響應的)。因此,此代碼用於顯示覆選標記和選擇顏色,並在選擇或取消選擇其他單元格時正確更改該狀態。還請注意,我出院細胞,因爲這是更高的內存效率,然後你創建你的細胞的方式,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
    cell.textLabel.font = [UIFont systemFontOfSize: 15.0f]; 
    cell.textLabel.text = [data objectAtIndex:indexPath.row]; 

    if([_selectedInts containsObject:@(indexPath.row)]){ 
     [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; 
     [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; 
    } else { 
     [cell setAccessoryType:UITableViewCellAccessoryNone]; 
    } 
    return cell; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    [self.selectedInts addObject:@(indexPath.row)]; 
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; 
} 


-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [self.selectedInts removeObject:@(indexPath.row)]; 
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; 
} 
+0

真棒!非常感謝 – stackOverFlew

0

在cellForRowAtIndexPath方法中,是否真的需要在單元格可見時創建新的單元格?

讓我們試試,這是蘋果推薦的最佳做法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 
cell.textLabel.text = @"a"; 
return cell; 
} 

希望這有助於你