Ios 默认情况下,设置UiTableView复选框
我想创建一个功能,应用程序应该自动选中uitableview复选标记,并且用户也可以手动选择单元格Ios 默认情况下,设置UiTableView复选框,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我想创建一个功能,应用程序应该自动选中uitableview复选标记,并且用户也可以手动选择单元格 #pragma mark - Table View Data Source - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath { static NSString *unifiedID = @"aCellID";
#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;
}
手动选择电池时,此代码正常工作。应选择的电池数据可存储在阵列中。请分享一些想法?它以以下方式工作。表示只需反转附件类型即可
- (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;
}
您的复选框编码boss在哪里,请使用allowmultipleselection作为检查位置box@Anbu.Karthikcellforrow中的isRowSelectedOnTableView方法用于单元格检查。如何使用IndExpath预加载self.selectedCells数组?您是否考虑过使用NSMutableIndexSet?@Paulw11 self.selectedcells在用户点击单元格时填充索引。表示只需反转附件类型即可