繁体   English   中英

iOS UITableViewCell添加标签和图像

[英]iOS UITableViewCell adding label and image

我正在尝试在UITableViewCell中添加文本标签或UIImage(无关紧要)。 但是它没有出现。 我在该单元格中使用的视图很少。 您可以在下面的代码中看到它:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"timelineCell";
        KMWTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        if (cell)
        {
            cell = [cell initWithFrame:CGRectMake(13, 10, 293, 200)];

            UIView *cellContentView = [[UIView alloc] initWithFrame:CGRectMake(13, 10, 293, 200)];
            cellContentView.layer.cornerRadius = 5;
            cellContentView.layer.shadowOffset = CGSizeMake(1, 0);
            cellContentView.layer.shadowColor = [[UIColor blackColor] CGColor];
            cellContentView.layer.shadowRadius = 5;
            cellContentView.layer.shadowOpacity = .25;
            cellContentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cellBack"]];


            UIView *selectedContentView = [[UIView alloc] initWithFrame:CGRectMake(13, 10, 293, 200)];
            selectedContentView.layer.cornerRadius = 5;
            selectedContentView.layer.shadowOffset = CGSizeMake(1, 0);
            selectedContentView.layer.shadowColor = [[UIColor blackColor] CGColor];
            selectedContentView.layer.shadowRadius = 5;
            selectedContentView.layer.shadowOpacity = .25;
            selectedContentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cellBackActive"]];


            UIView *visibleContentView = [[UIView alloc] initWithFrame:CGRectMake(13, 10, 293, 200)];
            visibleContentView.layer.cornerRadius = 5;
            visibleContentView.layer.shadowOffset = CGSizeMake(1, 0);
            visibleContentView.layer.shadowColor = [[UIColor blackColor] CGColor];
            visibleContentView.layer.shadowRadius = 5;
            visibleContentView.layer.shadowOpacity = .25;
            visibleContentView.backgroundColor = [UIColor clearColor];
//Image adding here:
 [ UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"active.png"]];
        [visibleContentView addSubview:image];

            [cellContentView addSubview:visibleContentView];
            [selectedContentView addSubview:visibleContentView];

            [cell setBackgroundView:cellContentView];
            [cell setSelectedBackgroundView:selectedContentView];

        }

        return cell;
    }

如您所见,我正在使用backgroundView和selectedBackgroundView,还使用clearColor查看其他信息。 那么,为什么其他元素不显示在visibleView中呢?

更新:

我正在使用子类,因为我需要更改单元格的帧大小。

我认为问题在于,您正在将visibleContentView添加到selectedContentView然后将其添加到cellContentView 这会将其从cellContentView删除,因为一个视图一次只能有一个cellContentView视图。 因此visibleContentView实际上位于selectedContentView 如果KMWTableViewCell正常工作,则在选择单元格时您应该会看到缺少的组件。

您可以尝试更改“ KMWtableViewCell”行吗

KMWTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

我认为KMWTableViewCell可能会造成问题。

我真的不知道这是否是正确的答案,因为我离开了Mac,但我认为您应该更换

KMWTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

KMWTableViewCell *cell = (KMWTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

希望对您有所帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM