簡體   English   中英

在自定義uitableviewcell中單擊一個uibutton會在uitableview中選擇多個uibutton

[英]clicking on one uibutton in custom uitableviewcell selects multiple uibuttons in uitableview

我在uiviewcontroller的childViewController中有一個uitableview。 在該uitableview中,我使用了自定義uitableviewcell。 在該自定義uitableviewcell中,我有“收藏夾”復選框按鈕。

該uitable視圖包含大約30條記錄,大約10條記錄出現在可見屏幕上,然后滾動顯示另外10條,然后滾動另一條。

問題是當我在第一行選擇一個uibutton,然后向下滾動uitableview時,我也看到它在第11和21行也被選中。 我已經忙了半天了,但是找不到問題。 請幫忙。

這是cellForRowAtIndexPath代碼:

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

ABCViewCell *cell = (ABCViewCell*)[tableView dequeueReusableCellWithIdentifier:@"ABCViewCell" forIndexPath:indexPath];
cell.delegate = self;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.layoutMargins = UIEdgeInsetsZero;
cell.labelDesiredDate.text = [dateFormatter stringFromDate:[NSDate date]];
cell.labelJournaledDate.text = [dateFormatter stringFromDate:[NSDate date]];

return cell; }

謝謝阿卜杜勒

您必須記住,當您使用dequeueReusableCellWithIdentifier方法時,您的單元格將成為繪制所有內容的開始記錄。 因此,您必須考慮一種清除數據的方法。

如下所示:

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

    ABCViewCell *cell = (ABCViewCell*)[tableView dequeueReusableCellWithIdentifier:@"ABCViewCell" forIndexPath:indexPath];
    if ([self shouldMyCellBeSelected]) {
        [cell displayButtonSelected];
    } else {
        [cell displayButtonUnselected];
    }
    return cell;
}
  1. shouldMyCellBeSelected 您應該具有確定是否應選擇給定位置的特定單元格的功能。 您可以通過為用戶單元格選擇創建一個單獨的索引列表,然后比較當前要繪制的單元格以查看其是否在該列表中,來實現此目的。
  2. displayButtonSelected / displayButtonUnselected 然后,您應該具有一個功能,用於顯示“已選擇”和“未選擇”單元格。 這可以通過更改按鈕的圖像或激活/禁用它來實現。

希望能有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM