Ios 检查按钮是否已在Objective-C中收到触摸
我想知道我是否在这段代码中做错了什么:Ios 检查按钮是否已在Objective-C中收到触摸,ios,objective-c,Ios,Objective C,我想知道我是否在这段代码中做错了什么: - (void)checkButtonTapped:(id)sender event:(id)event { NSSet *touches = [event allTouches]; UITouch *touch = [touches anyObject]; CGPoint currentTouchPosition = [touch locationInView:tableView]; NSLog(@"Current: ",
- (void)checkButtonTapped:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:tableView];
NSLog(@"Current: ", currentTouchPosition);
NSIndexPath *indexPath = [tableView indexPathForRowAtPoint: currentTouchPosition];
NSLog(@"Index Path: ", indexPath);
if (indexPath != nil)
{
[self tableView: tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}
}
indexPath和currentTouchPosition为空
我在这里调用方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *sectionTitle = [artistSectionTitles objectAtIndex:indexPath.section];
NSMutableDictionary *sectionArtists = [artists objectForKey:sectionTitle];
NSString *title = [sectionArtists objectForKey: [NSString stringWithFormat:@"%i", indexPath.row]];
NSMutableDictionary *artist = [baseController getCurrentItemByTitle: artistsBD :title];
NSInteger ID = [artist objectForKey: @"1"];
static NSString *cellIdentifier = @"listArtists";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[tableView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:@"listArtists"];
cell = [tableView dequeueReusableCellWithIdentifier:@"listArtists"];
cell.lbName.text = [artist objectForKey: @"2"];
NSInteger *qtdAlbuns = [artistDAO countAlbuns: ID];
cell.lbDetails.text = [NSString stringWithFormat:@"%i álbuns", qtdAlbuns];
cell.icon.image = [UIImage imageNamed: @"icones-categorias-artista"];
cell.imagePricipal.image = [artistDAO getThumb: ID];
UIImage *image = [UIImage imageNamed:@"icon-up"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
//cell.accessoryView = [[UIImageView alloc ] initWithImage:[UIImage imageNamed:@"icon-up"]];
[self setImageCornerRadius : cell];
cell.tag = ID;
return cell;
}
按钮的img:
当我点击按钮到currentTouchPosition并且indexPath为空时,我想知道我是否在代码中做了什么错误。实现所需的一个更简单的方法是将UIButton与块一起使用,如explained 这样,您就不需要checkButtonTapped:方法,只需执行以下操作即可代替[button addTarget…]:
[button handleControlEvent:UIControlEventTouchUpInside
withBlock:^{
[self tableView:tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}];
尝试从按钮向上移动视图层次结构:
static UITableViewCell *cellAncestorOfView(UIView *view) {
while (YES) {
if (view == nil || [view isKindOfClass:[UITableViewCell class]]) {
return view;
}
view = view.superview;
}
}
- (void)checkButtonTapped:(UIButton *)sender event:(UIEvent *)event {
UITableViewCell *cell = cellAncestorOfView(sender);
if (cell != nil) {
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
if (indexPath != nil) {
[self tableView:tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
}
}
}
你到底在干什么?您需要每个tableview单元格都有相同的按钮&该按钮的效果相同吗?单击该按钮将展开艺术家的相册,明白吗*很抱歉我的英语不好,我建议你创建自己的UITableViewCell类。然后在自定义单元格类中,您可以创建按钮。当点击特定单元格视图中的特定按钮时,您可以调用自定义UITableViewCell类到视图控制器的委托。解决了,我选择了错误的UITableView。谢谢大家