2013-07-19 89 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