我目前正在开发一个UITableView
,其中带有复选标记的选项。 问题是我需要对一次选择多少项目进行限制。复选标记单选
例如:
NO洋葱,NO盐,YES牛奶,NO桔子。
如本例所示,我需要一个限制,以便用户只能选择一个项目。
目前代码:
-(void)checkButtonPressed:(id)sender event:(id)event {
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.myTableView];
indexP = [self.myTableView indexPathForRowAtPoint: currentTouchPosition];
NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
//MasterCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"MasterCell"];
item = [arrayData objectAtIndex:indexP.row];
if (indexP != nil) {
[self tableView: self.myTableView accessoryButtonTappedForRowWithIndexPath:indexP];
}
[self.myTableView reloadData];
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
NSMutableDictionary *item = [arrayData objectAtIndex:indexPath.row];
BOOL checked = [[item objectForKey:@"checked"] boolValue];
[item setObject:[NSNumber numberWithBool:!checked] forKey:@"checked"];
UITableViewCell *cell = [item objectForKey:@"cell"];
UIButton *button = (UIButton *)cell.accessoryView;
UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
NSLog(@"accessoryButtonTappedForRowWithIndexPath____");
}
我的cellForRowAtIndexPath:
- (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];
}
NSMutableDictionary *item = [arrayData objectAtIndex:indexPath.row];
UILabel *nomeLabel = (UILabel *)[cell viewWithTag:2];
UIButton *btnCheck = (UIButton *)[cell viewWithTag:3];
BOOL checked = [[item objectForKey:@"checked"] boolValue];
UIImage *image = (checked) ? [UIImage imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"];
[btnCheck setBackgroundImage:image forState:UIControlStateNormal];
nomeLabel.text = [item valueForKey:@"nome"];
nomeLabel.textColor = [UIColor blackColor];
[btnCheck addTarget:self action:@selector(checkButtonPressed:event:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
在此先感谢和抱歉,我的英语不好,请不要往下投票,只问如果您有任何疑问,我总是在附近回答。
你需要什么?单选或多选表列表项? – 2013-03-18 10:19:36
单独选择:) – Bruno 2013-03-18 10:34:15
我已经与选中未选中的图片共享自定义单元格示例 – 2013-03-18 10:45:11