首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >contentView标签问题

contentView标签问题
EN

Stack Overflow用户
提问于 2011-09-01 00:42:14
回答 2查看 368关注 0票数 0

我创建了contentViews并分配了标签。当我在模拟器中点击row时,我不能得到正确的标签值。标记值始终为0。我的代码出了什么问题?

代码语言:javascript
复制
- (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");
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-09-01 00:48:49

您正在查询contentview的标记,但是将该标记添加到contentview的子视图中。

代码语言:javascript
复制
CellView
    ContentView    //You're asking for the tag of this
        Subview    //But assigning the tag to this

试试((UIView*)[cell.contentview subviews] objectAtIndex:0]).tag

票数 1
EN

Stack Overflow用户

发布于 2011-09-01 00:50:34

cell.contentView.tag返回单元格contentView的标记,但是在您的代码中,您设置了contentView的子视图(图像视图)的标记。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7260441

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档