我想创建应用程序应该以自动选择的uitableview复选标记开头的功能,此外用户还可以手动选择单元格
#pragma mark - Table View Data Source
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)
indexPath {
static NSString *unifiedID = @"aCellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:unifiedID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:unifiedID];
}
//if the indexPath was found among the selected ones, set the checkmark on the cell
cell.accessoryType = ([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
[cell.textLabel setText:[categoryList objectAtIndex:indexPath.row]];
[cell.imageView setImage:[categoryImagesArr objectAtIndex:indexPath.row]];
return cell;
}
//if a row gets selected, toggle checkmark
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *catStr = [categoryIdArr objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]){
[self.selectedCells removeObject:indexPath];
[self.selecedcategories removeObject:catStr];
cell.accessoryType = UITableViewCellAccessoryNone;
}else{
[self.selectedCells addObject:indexPath];
[self.selecedcategories addObject:catStr];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
-(BOOL)isRowSelectedOnTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath{
return ([self.selectedCells containsObject:indexPath]) ? YES : NO;
}
此代码可以正常手动选择单元格。应该选择的单元格数据可以存储在数组中。请分享一些想法?
答案 0 :(得分:0)
它按以下方式工作。只需反转附件类型
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)
indexPath {
static NSString *unifiedID = @"aCellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:unifiedID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:unifiedID];
}
//if the indexPath was found among the selected ones, set the checkmark on the cell
// cell.accessoryType = ([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
// NSLog(@"yes or no:::%hhd",[self isRowSelectedOnTableView:tableView atIndexPath:indexPath]);
if ([self isRowSelectedOnTableView:tableView atIndexPath:indexPath])
{
cell.accessoryType=UITableViewCellAccessoryNone;
}
else{
cell.accessoryType=UITableViewCellAccessoryCheckmark;
}
[cell.textLabel setText:[categoryList objectAtIndex:indexPath.row]];
// [cell.imageView setImage:[categoryImagesArr objectAtIndex:indexPath.row]];
return cell;
}