2015-09-30 41 views
0

我在簡單列表應用程序(iOS)中遇到了一些非常奇怪的行爲,這些行爲與我的tableViewCells有關。目標C - UITableViewCell中的ImageView(自定義複選標記)在多個單元格選擇後停止切換

這是我的工作應用程序的基本功能:
TableView有2個部分。第一部分(例如2個單元格)始終可見。第二部分可以用部分標題中的自定義按鈕顯示/隱藏。我創建了兩個類(即FirstSectionItem和SecondSectionItem),它們都具有布爾屬性「completed」和一些其他屬性。

編譯後,應用程序按預期運行。點擊單元格會顯示覆選標記並再次點擊它們隱藏複選標記(=自定義imageView)。然而,在點擊某些不同的單元格(隨機順序)或顯示/隱藏第二部分後,無論我點擊多少單元格,複選標記(ImageViews)都傾向於保持檢查狀態。過了一會兒,每個單元格都被檢查並且不能被取消選中,但布爾值仍然保持不變。

下面是部分代碼:

@property NSMutableArray *sectionTitleArray; 
@property NSMutableDictionary *sectionContentDict; 
@property NSMutableArray *firstSectionItems; 
@property NSMutableArray *secondSectionItems; 

viewDidLoad中:

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    if (!self.sectionTitleArray) { 
     self.sectionTitleArray = [NSMutableArray arrayWithObjects:@"section1", @"section2", nil]; 
    } 

    self.firstSectionItems = [[NSMutableArray alloc] init]; 
    self.secondSectionItems = [[NSMutableArray alloc] init]; 
    [self loadInitialData]; 

    self.sectionContentDict = [[NSMutableDictionary alloc] init]; 

    self.arraySection1 = [[NSMutableArray alloc] init]; 

    for (int i = 0; i < [self.firstSectionItems count]; i++) 
     { 
      FirstSectionItem *firstSectionItem = [self.firstSectionItem objectAtIndex:i]; 
      [self.arraySection1 addObject:firstSectionItem.itemName]; 
     } 

    self.arraySection2 = [[NSMutableArray alloc] init]; 
    for (int i = 0; i < [self.secondSectionItems count]; i++) 
     { 
      SecondSectionItem *secondSectionItem = [self.secondSectionItems objectAtIndex:i]; 
      [self.arrayFuture addObject:secondSectionItem.itemName]; 
     } 

    [self.sectionContentDict setValue:self.arraySection1 forKey:[self.sectionTitleArray objectAtIndex:0]]; 
    [self.sectionContentDict setValue:self.arraySection2 forKey:[self.sectionTitleArray objectAtIndex:1]]; 
} 

的cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListPrototypeCell" forIndexPath:indexPath]; 

    // ... cell content ... 

    NSArray *content = [self.sectionContentDict valueForKey:[self.sectionTitleArray objectAtIndex:indexPath.section]]; 

    //... constraints ... 

    UIImage *checkmark = [UIImage imageNamed:@"checkmark.png"]; 
    UIImage *noCheckmark = [UIImage imageNamed:@"transparent.png"]; 
    UIImageView *imageView = [[UIImageView alloc] init]; 

    if (indexPath.section==0){ 

      FirstSectionItem *firstSectionItem = [self.firstSectionItems objectAtIndex:indexPath.row]; 
      imageView.image = firstSectionItem.completed ? checkmark : noCheckmark; 

} 


    if (indexPath.section==1){ 
     if(self.sec2isTapped == YES){ 
      SecondSectionItem *secondSectionItem = [self.secondSectionItems objectAtIndex:indexPath.row]; 
      imageView.image = secondSectionItem.completed ? checkmark : noCheckmark; 
     } 
     } 

    [cell.contentView addSubview:imageView]; 

    // ... more constraints 


return cell; 
} 

didSelectRowAtIndexPath方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [tableView deselectRowAtIndexPath:indexPath animated:NO]; 

    if (indexPath.section ==0){ 
     FirstSectionItem *tappedItem = [self.firstSectionItems objectAtIndex:indexPath.row]; 
     tappedItem.completed = !tappedItem.completed; 
    } 
    if (indexPath.section ==1){ 
     SecondSectionItem *tappedItem = [self.secondSectionItems objectAtIndex:indexPath.row]; 
     tappedItem.completed = !tappedItem.completed; 
    } 

    [tableView beginUpdates]; 
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle]; 
    [tableView endUpdates]; 

} 

我已經嘗試了多種方法,但他們都沒有工作。我無法弄清楚問題所在。

(我是新來的目標C,所以有些部分可能看起來有點迂迴)。

在此先感謝。

回答

1

問題是,您每次運行cellForRowAtIndexPath時都會初始化UIImageView。但請記住,單元格被重用,因此您正在重複使用具有複選標記的單元格,並在頂部添加一個新的清除複選標記。

你需要做的是兩種:

  1. 添加的ImageView在故事板,標記並用戶標籤發現它的代碼
  2. 子類的UITableViewCell,並在那裏分配正確的圖像。

有很多如何在網上做文章。

+0

謝謝!讓我們做一些閱讀。 – Baekel

相關問題