Iphone 在表格视图的单元格内处理图像和标签
我已经在表视图的每个单元格中创建了标签和图像。如果单击图像,我希望标签数据引用到该单元格Iphone 在表格视图的单元格内处理图像和标签,iphone,uitableview,uiimageview,Iphone,Uitableview,Uiimageview,我已经在表视图的每个单元格中创建了标签和图像。如果单击图像,我希望标签数据引用到该单元格 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UILabel *phone=[[[UILabel alloc]initWithFrame:CGRectMake(10, 95, 320,15)]autorelease]; phon
- (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);
}
但是如何获取标签数据呢
谢谢你你的代码似乎有几处错误,比如你在cellForRowAtIndexPath中的“cell”在哪里?也就是说,要解决眼前的问题,您可以在CellForRowatineXpath内的手机标签上添加一个标签,例如
phone.tag=5
然后在手势识别器方法中,添加UILabel*phone=(UILabel*)[cell viewWithTag:5]代码>。现在您有了对该单元格中标签的引用。i每行我都有一个图像和标签。每行中的标签数据不同
- (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);
}