处理表格视图的单元格内的图像和标签

时间:2013-04-03 21:28:04

标签: iphone uitableview uiimageview

我在表格视图的每个单元格中创建了一个标签和图像。如果点击图像,我想要引用该单元格的标签数据。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UILabel *phone=[[[UILabel alloc]initWithFrame:CGRectMake(10, 95, 320,15)]autorelease];

    phone.font=[UIFont boldSystemFontOfSize:12];
    [phone setTextAlignment:UITextAlignmentLeft];
    [phone  setText:[d valueForKey:@"Phone"]];
    [cell addSubview:phone];

    myImageView.image = [UIImage imageNamed:@"call.png"];
    cell.imageView.image=myImageView.image;
    cell.imageView.userInteractionEnabled=YES;

    UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageSelectedInTable:)];

    [cell.imageView addGestureRecognizer:tapped];
    [tapped release];
}

-(IBAction)imageSelectedInTable:(UITapGestureRecognizer*)gesture
{
    NSLog(@"Selected an Image");

    UIImageView *imgView = (UIImageView *) [gesture view];
    UITableViewCell *cell = (UITableViewCell*)[[imgView superview]superview];
    NSIndexPath *tappedIndexPath = [self.tableView indexPathForCell:cell];
    NSLog(@"Image Tap %@", tappedIndexPath);
}

但如何获取标签数据 谢谢你

1 个答案:

答案 0 :(得分:0)

您的代码似乎有些问题,例如您在cellForRowAtIndexPath中获取“cell”的位置?话虽这么说,为了解决您的问题,您可以在cellForRowAtIndexPath中的手机标签上添加标签,例如phone.tag = 5;然后在您的手势识别器方法中添加UILabel *phone = (UILabel *)[cell viewWithTag:5];。现在您可以参考该单元格中的标签。