0

我正在尝试在 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 中呢?

更新:

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

4

3 回答 3

1

我认为问题在于您在将其添加visibleContentView到. 这将其删除,因为视图一次只能有一个超级视图。所以实际上是在. 如果工作正常,您应该在选择单元格时看到缺少的组件。selectedContentViewcellContentViewcellContentViewvisibleContentViewselectedContentViewKMWTableViewCell

于 2013-07-19T13:45:34.317 回答
0

您可以尝试在线更改“KMWtableViewCell”吗

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

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

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

于 2013-07-19T13:47:48.250 回答
0

我真的不知道这是否是正确的答案,因为我不在我的 Mac 上,但我认为你应该更换

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

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

希望它有所帮助!

于 2013-07-19T14:02:29.660 回答