您需要以编程方式将按钮添加到单元格或创建自定义单元格。当用户点击单元格或复选框时,您可以将按钮的图像更改为选中的复选框。为了正确显示复选框,请使用 NSMutableArray 并使用以下代码。
笔记
1. specialNeedsList is a array which I'm using to display data on cell.
2. selectedSpecialNeed is a Mutable Array which I'm using to store selected/checked values
3. I'm using checkbox button for UITableViewCell accessory view. IN your case code will be little bit different.
按照本教程获取点击按钮的行以及我的代码。你会得到实现这一点的想法。无论我在做什么, - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
您都需要在 UIButton 的操作上执行此操作。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger returnValue = 1;
return returnValue;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger returnValue = 0;
returnValue = ([self.specialNeedsList count] > 0) ? [self.specialNeedsList count]:0;
return returnValue;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UIButton * btnShare = [[UIButton alloc] initWithFrame:CGRectMake(0,0,30,30)];
[btnShare setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
[btnShare setImage:[UIImage imageNamed:@"checkboxChecked.png"] forState:UIControlStateHighlighted];
[btnShare setImage:[UIImage imageNamed:@"checkboxChecked.png"] forState:UIControlStateSelected];
[btnShare addTarget: self action: @selector(accessoryButtonTapped:withEvent:)
forControlEvents: UIControlEventTouchUpInside];
cell.accessoryView = btnShare;
[btnShare release];
}
NSString* subAreaOfStudy = [self.specialNeedsList objectAtIndex:indexPath.row];
if (self.specialNeedsList && [self.specialNeedsList count] > 0) {
[cell.textLabel setFont:[UIFont systemFontOfSize:15.0f]];
cell.textLabel.text = [self.specialNeedsList objectAtIndex:indexPath.row];
}
BOOL isShared = ([self.selectedSpecialNeeds count] > 0 && ([self.selectedSpecialNeeds indexOfObject:subAreaOfStudy] != NSNotFound));
UIButton* btnShare = (UIButton*)cell.accessoryView;
btnShare.selected = isShared;
[btnShare setNeedsDisplay];
return cell;
}
- (void) accessoryButtonTapped: (UIControl *) button withEvent: (UIEvent *) event{
NSIndexPath * indexPath;
indexPath = [tblVSpecialNeeds indexPathForRowAtPoint: [[[event touchesForView: button] anyObject] locationInView:tblVSpecialNeeds]];
if ( indexPath == nil )
return;
[self tableView:tblVSpecialNeeds accessoryButtonTappedForRowWithIndexPath:indexPath];
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
NSString *draft = [self.specialNeedsList objectAtIndex:indexPath.row];
BOOL recordAlreadySelected = ([self.selectedSpecialNeeds indexOfObjectIdenticalTo:draft] != NSNotFound);
if(recordAlreadySelected) {
[self.selectedSpecialNeeds removeObject:draft];
}
else {
[self.selectedSpecialNeeds addObject:draft];
}
[tblVSpecialNeeds reloadData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row > [self.specialNeedsList count]-1) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
return;
}
[self tableView:tblVSpecialNeeds accessoryButtonTappedForRowWithIndexPath:indexPath];
}