获取带勾号的单元格的标签

我正在与桌面视图。 我在屏幕的第一个水龙头上添加了复选标记附件,并在第二个水龙头上删除了这个复选标记。 我在表格视图的单元格上添加了一些ckeckmarks。 现在,我希望具有选中标记的单元格的标签应显示在NSlog上。 请帮我解决这个问题。 任何帮助将是非常appriciable。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView.tag == 0) { return [celltitles count]; } else { return 7; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } if (tableView.tag == 0) { cell.textLabel.text = [celltitles objectAtIndex:indexPath.row]; if (indexPath.row == 0) { habitname = [[UITextField alloc]initWithFrame:CGRectMake(150, 0, 150, 50)]; habitname.placeholder = @"Habit Name"; habitname.delegate= self; [cell addSubview:habitname]; } else if (indexPath.row == 1) { timelbl = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 50)]; timelbl.text = @"OFF"; timelbl.textAlignment = NSTextAlignmentCenter; [cell addSubview:timelbl]; timetitlelbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)]; timetitlelbl.text = @"Alert"; timetitlelbl.textAlignment = NSTextAlignmentCenter; [cell addSubview:timetitlelbl]; toggleswitch = [[UISwitch alloc] initWithFrame:CGRectMake(250, 5, 50, 60)]; toggleswitch.on = NO; [toggleswitch addTarget:self action:@selector(toggleSwitch) forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)]; [cell addSubview:toggleswitch]; } } else if (tableView.tag == 1) { cell.textLabel.text = [daysarray objectAtIndex:indexPath.row]; } return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == 1) { if (toggleswitch.on) { [self datepickershown:timelbl.text animated:YES]; if (self.datePickerIsShowing) { //[self datepickershown]; //[self hideDatePickerCell]; [dscptxt resignFirstResponder]; } else { [dscptxt resignFirstResponder]; [self.timelbl resignFirstResponder]; [self showDatePickerCell]; } } else { timelbl.textColor = [UIColor grayColor]; } } else if (indexPath.row > 2 && indexPath.row <10) { NSLog(@"I am Selectin the row"); UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if (cell.accessoryType == UITableViewCellAccessoryNone) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { // NSLog(@"Selected Accessory Is %d",cell.accessoryType); cell.accessoryType = UITableViewCellAccessoryNone; } } [self.tableview deselectRowAtIndexPath:indexPath animated:YES]; } - (void)savedata:(id)sender { } 

以上是我正在使用的代码和保存行动我想要的日志。

不要使用UITableView来存储你的数据。 单元格被重用,所以这不是一个可靠的方法来检测您select的项目。

例如,您可以存储是否在数组中select某个项目。 然后在你的cellForRowAtIndexPath确定是否应该显示复选标记。 在didSelectRowAtIndexPath您更新模型(您的数组)并请求重新加载特定的单元格。

这将导致更好的MVC分离。

编辑 :添加UITableView东西的例子

 // for this example we'll be storing NSIndexPath objects in our Model // you might want to use a @property for this NSMutableSet *model = [NSMutableSet set]; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ... if([model containsObject:indexPath]) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } ... } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { ... // update the model if([model containsObject:indexPath]) { [model removeObject:indexPath]; } else { [model addObject:indexPath]; } // request reload of selected cell [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation: UITableViewRowAnimationFade]; ... } 

现在应该很容易logging所有select的项目,而不必使用UITableView :现在就在你的模型中!

做一个for循环:

 NSMutableArray *mutArray = [[NSMutableArray alloc] init]; for(i = 0;i <= self.rowCount-1;i++) { UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]; if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { [mutArray addObject:[NSIndexPath indexPathForRow:i inSection:0]]; } } NSLog(@"Array: %@", mutArray);