2013-07-19 91 views
0

我想在UITableViewCell中添加文本标签或UIImage(无所谓)。 但它没有出现。我在这个单元格中使用了很少的视图。你可以看到它在下面的代码:iOS UITableViewCell添加标签和图像

- (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中显示呢?

UPDATE:

我使用的子类,因为我需要改变细胞的帧大小。

+2

如果您使用的是“UITableViewCell”子类,您为什么还在做这一切? – Desdenova

+1

这不是关于问题,但为什么要将'visibleContentView'添加到'cellContentView'和'selectedContentView'两个视图中。你为什么不简单地将它添加到'selectedContentView'中。 –

+1

我认为这是问题! –

回答

1

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

+0

你是对的!但是我需要'visibleContentView'在用户点击单元格时不会消失。所以我需要用相同的内容创建两个不同的视图,一个用于'cellContent'视图,另一个用于'selectedContentView'? –

+0

是的,这应该工作。但我不认为它会消失。你试过了吗? –

0

你可以尝试线

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

改变 “KMWtableViewCell” 到

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

我想KMWTableViewCell可能会产生问题。

+0

我不能,只是因为我需要改变单元格框架的大小,它只能通过创建子类 –

0

我真的不能知道这是否是正确的答案,因为我是从我的MAC了,但我想你应该更换

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

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

希望它帮助!

+0

Tnx来获得答案,但原因是我试图在两个视图中添加子视图 –

+0

哦,好吧,至少我试过了: d – JomanJi