我創建了contentViews和分配的標記。當我在模擬器中單擊行時,我無法獲得正確的標記值。標記值始終爲0. 我的代碼有什麼問題?contentView標記問題
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIImageView *imgView, *imgView1;
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text = @"test";
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100,0,20,62)];
[imgView setImage:[UIImage imageNamed:@"1.png"]];
imgView.tag = 10;
[cell.contentView addSubview:imgView];
[imgView release];
imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(200,0,20,62)];
[imgView1 setImage:[UIImage imageNamed:@"2.png"]];
imgView1.tag = 20;
[cell.contentView addSubview:imgView1];
[imgView1 release];
}
else
{
imgView = (id)[cell.contentView viewWithTag:10];
imgView1 = (id)[cell.contentView viewWithTag:20];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.contentView.tag == 10) // tag is always 0. What's wrong?
NSLog(@"10");
else if (cell.contentView.tag == 20)
NSLog(@"20");
}
沒錯。我有另一個問題。如何知道是否觸摸了左側內容視圖或觸摸了正確的內容視圖? – user698200
我的迴應中的代碼只返回第一個子視圖。你可以解析所有這些:'for(UIView * view in [cell.contentView subviews]){int tag = view.tag; }' –
我仍然不知道哪個視圖被觸摸。也許我必須發表另一個問題。 – user698200