选择UITableViewCell AccessoryView,与选择行分开 [英] Selecting UITableViewCell AccessoryView, separate from selecting row
问题描述
我有一个UITableview
,我正在显示一些任务,每一行都有一个复选框,用于标记任务是否完成.
I have a UITableview
that I'm displaying some tasks and each row has a checkbox to mark tasks complete or not.
我希望在用户点击复选框时切换选中标记,并在用户点击行时切换到详细视图.后者很简单,只需使用
I'm looking to toggle the checkmark when the user taps on the checkbox, and switch to a detailed view when the user taps on the row. The latter is easy, just by using
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
然而,我想选择区域分开,切换仅在选择accessoryview
内添加UIbutton
,则当用户只想单击复选框时,用户将选择行和UIButton
,对吗?
However, I wanted to separate the selection areas, toggling only the checkbox when the accessoryview
is selected, and going into detailed view only when the rest of the cell is selected. If I add a UIbutton
inside the accessoryview
, the user would be selecting the row AND the UIButton
when they only wanted to hit the checkbox, right?
此外,如果用户只是通过沿accessoryview
拖动来滚动浏览表格视图怎么办?这样是否会触发对TouchUp上的UIButton
的操作?
Also, what if the user was just scrolling through the tableview by dragging along the accessoryview
? Wouldn't this trigger an action on the UIButton
on TouchUp?
有人对如何执行此操作有任何想法吗?感谢您的宝贵时间!
Anyone have any ideas on how to do this? Thanks for your time!
推荐答案
如何在此委托方法中管理附件水龙头:
How about managing accessory taps inside this delegate method:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
您可以对响应accessoryButtonTappedForRowWithIndexPath:
方法的自定义AccessoriesView做类似的事情.
You can do something like this for custom accessoryView that responds to accessoryButtonTappedForRowWithIndexPath:
method.
在cellForRowAtIndexPath:
方法中-
BOOL checked = [[item objectForKey:@"checked"] boolValue];
UIImage *image = (checked) ? [UIImage imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
- (void)checkButtonTapped:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
if (indexPath != nil)
{
[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *item = [dataArray 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];
}
这篇关于选择UITableViewCell AccessoryView,与选择行分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!