我有一個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];
我希望這可以幫助別人。我一整天都在摔跤。
from where _checkedIndexPath from?你是否在課堂上存儲這個? – dminones
_checkdIndexPath只是跟蹤所選的最後一個NSIndexPath。是我的VC的它的和實例變量。 –
你可以顯示其餘的代碼?另外,你是否期望有多個複選標記可用?因爲我只看到你記錄一個檢查索引路徑。 – CrimsonChris