Iphone 单选复选标记
我目前正在开发一个Iphone 单选复选标记,iphone,ios,objective-c,uitableview,Iphone,Ios,Objective C,Uitableview,我目前正在开发一个UITableView,其中包含带有复选标记的可供选择的项目。 问题是我需要对一次选择的项目数量进行限制 例如: 没有洋葱,没有盐,有牛奶,没有桔子 如本例所示,我需要一个限制,以便用户只能选择一个项目 当前代码: -(void)checkButtonPressed:(id)sender event:(id)event { NSSet *touches = [event allTouches]; UITouch *touch = [touches anyObje
UITableView
,其中包含带有复选标记的可供选择的项目。
问题是我需要对一次选择的项目数量进行限制
例如:
没有洋葱,没有盐,有牛奶,没有桔子
如本例所示,我需要一个限制,以便用户只能选择一个项目
当前代码:
-(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____");
}
My 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;
}
提前感谢,并为我糟糕的英语感到抱歉,请不要否决投票,只要问问你们是否有任何疑问,我随时都会回答 基本上,对于这种业务逻辑,您必须自己编写代码。当使用表视图作为基本单选按钮时,强制执行单一选择要求的唯一方法是编写代码,以处理用户试图按照系统要求的方式选择第二项的情况:
- 允许多个选择(此处不是这种情况,但在某些情况下可能会)
- 删除与此不兼容的所有其他检查,检查所选项目,并告诉表视图刷新刚刚更改的所有行
- 以某种方式(眨眼、提醒、声音)向用户显示错误——如果允许直接操作,这几乎不是一个好主意
- 允许多个选择(此处不是这种情况,但在某些情况下可能会)
- 删除与此不兼容的所有其他检查,检查所选项目,并告诉表视图刷新刚刚更改的所有行
- 以某种方式(眨眼、提醒、声音)向用户显示错误——如果允许直接操作,这几乎不是一个好主意
- 基本上,对于这种业务逻辑,您必须自己编写代码。当使用表视图作为基本单选按钮时,强制执行单一选择要求的唯一方法是编写代码,以处理用户试图按照系统要求的方式选择第二项的情况:
nsindepath
变量
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* Cell = [tableView cellForRowAtIndexPath:indexPath];
int new = [indexPath row];
int old = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
if(new != old)
{
Cell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell* oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath = indexPath;
}
}
尝试这样做可能会对您有所帮助,这里lastIndexPath是在.h文件中声明的
nsindepath
变量
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* Cell = [tableView cellForRowAtIndexPath:indexPath];
int new = [indexPath row];
int old = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
if(new != old)
{
Cell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell* oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath = indexPath;
}
}
试试这个:
.h文件
@property(nonatomic, retain) NSIndexPath *lastIndexPath;
.m文件
int oldRow, newRow;
表格方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CustomCellIdentifier = @"customCell";
customCell *cell = (customCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
if (cell == nil)
{
NSArray *nib;
nib= [[NSBundle mainBundle] loadNibNamed:@"customCell" owner:self options:nil];
for (id oneObject in nib)
if ([oneObject isKindOfClass:[customCell class]])
cell = (customCell *)oneObject;
newRow = [indexPath row];
oldRow = [[self lastIndexPath] row];
cell.accessibilityViewIsModal = (newRow == oldRow && lastIndexPath != nil) ? NO : YES;
if(cell.accessibilityViewIsModal == NO)
cell.imgCheck.image = [UIImage imageNamed:@"Checkbox_Checked_New.png"];
else
cell.imgCheck.image = [UIImage imageNamed:@"Checkbox_Unchecked_New.png"];
return cell;
}
}
DidSelect::
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
newRow = [indexPath row];
oldRow = [[self lastIndexPath] row];
//Check Mark Feature
if (newRow != oldRow)
{
customCell *newCell = (customCell *)[tableView cellForRowAtIndexPath:indexPath];
newCell.imgCheck.image = [UIImage imageNamed:@"Checkbox_Checked_New.png"];
customCell *oldCell = (customCell *)[tableView cellForRowAtIndexPath:lastIndexPath];
oldCell.imgCheck.image = [UIImage imageNamed:@"Checkbox_Unchecked_New.png"];
[self setLastIndexPath:indexPath];
}
else
{
customCell *newCell = (customCell *)[tableView cellForRowAtIndexPath:indexPath];
newCell.imgCheck.image = [UIImage imageNamed:@"Checkbox_Checked_New.png"];
[self setLastIndexPath:indexPath];
}
}
试试这个:
.h文件
@property(nonatomic, retain) NSIndexPath *lastIndexPath;
.m文件
int oldRow, newRow;
表格方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CustomCellIdentifier = @"customCell";
customCell *cell = (customCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
if (cell == nil)
{
NSArray *nib;
nib= [[NSBundle mainBundle] loadNibNamed:@"customCell" owner:self options:nil];
for (id oneObject in nib)
if ([oneObject isKindOfClass:[customCell class]])
cell = (customCell *)oneObject;
newRow = [indexPath row];
oldRow = [[self lastIndexPath] row];
cell.accessibilityViewIsModal = (newRow == oldRow && lastIndexPath != nil) ? NO : YES;
if(cell.accessibilityViewIsModal == NO)
cell.imgCheck.image = [UIImage imageNamed:@"Checkbox_Checked_New.png"];
else
cell.imgCheck.image = [UIImage imageNamed:@"Checkbox_Unchecked_New.png"];
return cell;
}
}
DidSelect::
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
newRow = [indexPath row];
oldRow = [[self lastIndexPath] row];
//Check Mark Feature
if (newRow != oldRow)
{
customCell *newCell = (customCell *)[tableView cellForRowAtIndexPath:indexPath];
newCell.imgCheck.image = [UIImage imageNamed:@"Checkbox_Checked_New.png"];
customCell *oldCell = (customCell *)[tableView cellForRowAtIndexPath:lastIndexPath];
oldCell.imgCheck.image = [UIImage imageNamed:@"Checkbox_Unchecked_New.png"];
[self setLastIndexPath:indexPath];
}
else
{
customCell *newCell = (customCell *)[tableView cellForRowAtIndexPath:indexPath];
newCell.imgCheck.image = [UIImage imageNamed:@"Checkbox_Checked_New.png"];
[self setLastIndexPath:indexPath];
}
}
你需要什么??单选或多选表格列表项?我已将自定义单元格与选中未选中的图像共享example@Bruno你得到答案了吗?你需要什么??单选或多选表格列表项?我已将自定义单元格与选中未选中的图像共享example@Bruno你们得到答案了吗?我在这里取了带有imageview的自定义单元格,并设置了选中和未选中的图像。我在这里取了带有imageview的自定义单元格,并设置了选中和未选中的图像。