2016-01-06 24 views
-3

我有一個tableview,可以添加和刪除多個複選標記。唯一的問題是如果我把3個複選標記並滾動,當我返回時,複選標記消失了。我無法在互聯網上的任何地方找到可行的解決方案,並且我嘗試了多種變體,而且還是一無所獲。滾動時附件選中標記消失Objective-C

這是我在cellForRowAtIndex代碼,應持複選標記到位。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *reuseIdentifier = @"contactCell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; 
NSDictionary *contact = [self.tableData objectAtIndex:indexPath.row]; 



    UILabel *nameLabel = (UILabel *)[cell viewWithTag:1]; 
    NSString *firstName = contact[@"firstName"]; 
    nameLabel.text = [firstName stringByAppendingString:[NSString stringWithFormat:@" %@", contact[@"lastName"]]]; 

    UILabel *phoneNumber = (UILabel *)[cell viewWithTag:2]; 
    NSArray *phones = contact[@"phones"]; 

    if ([phones count] > 0) { 
     NSDictionary *phoneItem = phones[0]; 
     phoneNumber.text = phoneItem[@"value"]; 
    } 

    UIImageView *cellIconView = (UIImageView *)[cell.contentView viewWithTag:888]; 

    UIImage *image = contact[@"image"]; 

    cellIconView.image = (image != nil) ? image : [UIImage imageNamed:@"smiley-face"]; 
    cellIconView.contentScaleFactor = UIViewContentModeScaleAspectFill; 
    cellIconView.layer.cornerRadius = CGRectGetHeight(cellIconView.frame)/2; 




// Need to fix 
if([checkedIndexPath isEqual:indexPath]) 
{ 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 
else 
{ 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 


return cell; 
} 

這裏是didSelectRowAtIndexPath方法方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath 
    { 



UITableViewCell* checkCell = [tableView cellForRowAtIndexPath:indexPath]; 




if(checkCell.accessoryType == UITableViewCellAccessoryCheckmark) 
{ 
    checkCell.accessoryType = UITableViewCellAccessoryNone; 




    NSMutableArray *i = [[NSMutableArray alloc] init]; 
    for (NSIndexPath *indexPath in [self.tableView indexPathsForSelectedRows]) { 
     [i addObject:self.tableData[indexPath.row]]; 

     // Go inside pull the numbers from the users and save in an NSArray 
    // NSArray *contacts = i; 

    //  self.recipients = [[NSMutableArray alloc] init]; 


     for (NSDictionary* dict in i) { 

      // Grab phones 
      NSDictionary *contactNumber = [dict objectForKey:@"phones"]; 

      for (NSDictionary* dict2 in contactNumber) { 

       // Grabs the phone numbers 
       NSString* value = [dict2 objectForKey:@"value"]; 
       int index = [self.recipients indexOfObject:value]; 

       [self.recipients removeObjectAtIndex:index]; 
       [self.selectedUsers removeObjectAtIndex:index]; 
       NSLog(@"The number that has a checkmark%@", value); 
       NSLog(@"the array of all%@", self.recipients); 
       NSLog(@"At index %lu", (unsigned long)[self.recipients indexOfObject:value]); 
       // [_recipients addObject:value]; 
      } 

     } 
     // NSLog(@"Phone Numbers: %@",_recipients); 
    } 


} 

else 
{ 
    [self getNumber]; 

    NSLog(@"clicking %@", self.recipients); 

    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath]; 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    checkedIndexPath = indexPath; 


    } 
} 

我找到了解決辦法: 必須在每個indexPath保存到一個數組(放在didSelectRowAtIndexPath方法驗證碼),然後在的cellForRowAtIndexPath添加以下代碼

if([self.checkedCells containsObject:indexPath]) { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} else { 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 

也在didSelectRowAtIndexPath 確保在取消選擇該行時刪除indexPath。

if(checkCell.accessoryType == UITableViewCellAccessoryCheckmark) { 
    checkCell.accessoryType = UITableViewCellAccessoryNone; 
    [self.checkedCells removeObject:indexPath]; 

我希望這可以幫助別人。我一整天都在摔跤。

+0

from where _checkedIndexPath from?你是否在課堂上存儲這個? – dminones

+0

_checkdIndexPath只是跟蹤所選的最後一個NSIndexPath。是我的VC的它的和實例變量。 –

+0

你可以顯示其餘的代碼?另外,你是否期望有多個複選標記可用?因爲我只看到你記錄一個檢查索引路徑。 – CrimsonChris

回答

0

使checkedIndexPath a @property (nonatomic, strong)和使用self.checkedIndexPath每當你引用它。 didSelectRowAtIndexPath退出後,您將失去參考。在cellForRowAtIndexPath設置一個斷點,看看checkedIndexPath,我敢打賭它是零。

+0

你是對的,我確實沒有。這是爲什麼發生? –

+0

我告訴過你爲什麼 - 你沒有存儲對checkedIndexPath的引用,它超出了範圍。 – jsd

+0

感謝所有幫助的人。看看我想出的解決方案。 containsObject –

0

也許你應該檢查 isEqual功能做你的期望。你可以確保通過嘗試:

if (_checkedIndexPath.section == indexPath.section && 
    _checkedIndexPath.row  == indexPath.row) 

如果你還沒有得到預期的結果,也許是記錄的sectionrow值,看看哪裏出了問題。

請注意,如果由於某種原因_checkedIndexPath是弱變量或者被釋放,該檢查將失敗。

您也可以檢查您的單元在被修改之前是否已正確出列,並且您正在返回正確的單元格。

如果你想存儲多於一個checked行,當然,你將需要多個indexPath變量(只有一個_checkedIndexPath不會這樣做)。

+0

在'NSIndexPath'上使用'isEqual:'就好了。 – rmaddy

+0

鑑於所有其他有用的提示,我覺得反對票很苛刻。我也偶然發現要檢查的事情列表中的實際錯誤。 – Mundi

+0

不是我。我只是指出了一個問題。 – rmaddy