Cod*_*ody 8 iphone objective-c uitableview ios
我有一个UITableview我正在显示一些任务,每行都有一个复选框,用于标记任务是否完成.
我想在用户点击复选框时切换复选标记,并在用户点击该行时切换到详细视图.后者很容易,只需使用即可
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
但是,我想分离选择区域,仅在选中时切换复选框accessoryview,并仅在选择其余单元格时进入详细视图.如果我在UIbutton里面添加一个accessoryview,用户将选择行和UIButton他们只想点击复选框,对吗?
此外,如果用户只是通过拖动来滚动浏览tableview accessoryview怎么办?这不会触发UIButtonTouchUp 上的动作吗?
任何人对如何做到这一点都有任何想法?谢谢你的时间!
Ani*_*shi 17
如何管理此委托方法中的附件水龙头:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
编辑:
您可以对响应accessoryButtonTappedForRowWithIndexPath:方法的自定义accessoryView执行类似的操作.
在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];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8566 次 |
| 最近记录: |