【发布时间】:2015-08-28 11:41:17
【问题描述】:
我想知道我在这段代码中是否做错了什么:
- (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;
}
按钮图片:http://i.imgur.com/9svY63b.png
当我点击按钮到 currentTouchPosition 并且 indexPath 是空的,所以我想知道我是否在代码中做错了什么。
【问题讨论】:
-
你到底在做什么?您需要每个 tableview 单元格都具有相同的按钮并且该按钮的工作方式相同吗?
-
通过点击按钮会展开艺术家的专辑,明白吗? *对不起我的英语不好
-
我建议你创建自己的 UITableViewCell 类。然后在那个自定义单元格类中,您可以创建您的按钮。何时点击特定单元格视图中的特定按钮,您可以从自定义 UITableViewCell 类调用委托给您的视图控制器。
-
已解决,我走错了 UITableView。谢谢大家
标签: ios objective-c