iOS UITableViewCell 添加标签和图像

标签 ios objective-c

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

    - (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 添加到 cellContentView 后立即将其添加到 selectedContentView 中。这会将其从 cellContentView 中删除,因为一个 View 一次只能有一个 super View 。所以 visibleContentView 实际上位于 selectedContentView 中。如果 KMWTableViewCell 工作正常,则在选择单元格时您应该会看到缺少的组件。

关于iOS UITableViewCell 添加标签和图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17747359/

相关文章:

ios - 如何从开发、测试、登台和生产环境发送 APNS 推送通知?

iphone - 如何让两个ViewController使用一个NSObject

c++ - 如何在 Mac OSX 上的窗口客户端中获得真实的鼠标位置?

ios - cocos2d v3 覆盖清理

ios - Xcode:如何在用户选择语言时更改应用程序的语言?

ios - 当横向打开应用程序时,新创建的 UIWindow 是横向的

ios - Swift/iOS - 当设备恢复在线时将离线核心数据与服务器同步

objective-c - TrackingArea 中的 NSPopUpButton -> 从菜单中选择项目时鼠标退出

objective-c - 进行连续循环以从 iTunes 获取信息的最佳方式是什么?

ios - UIDatePicker 返回错误的日期(-1 天实际日期)