[英]handling image and label within cell of a table view
我已經在表格視圖的每個單元格中創建了標簽和圖像。 如果單擊圖像,我希望標簽數據引用該單元格。
- (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中的“單元格”? 話雖如此,為解決您的當前問題,您可以在cellForRowAtIndexPath內的電話標簽中添加標簽,例如phone.tag = 5;
然后在手勢識別器方法中,添加UILabel *phone = (UILabel *)[cell viewWithTag:5];
。 現在,您可以對該單元格中的標簽進行引用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.