2012-08-09 78 views
1

我有一個UITableView,其方法爲cellForRowAtIndexPath,如下所示,問題在於cellImage僅在完全向上滾動(因此沒有行可見)然後釋放後出現。奇怪的是,我在另一個類中有相同的代碼用於單獨的表視圖,並且圖像顯示正常。有任何想法嗎?單元格圖像僅在滾動後出現

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"CellIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    [cell.textLabel setText:[items objectAtIndex:indexPath.row]]; 
    [cell.textLabel setFont:[UIFont fontWithName:@"Helvetica" size:15.0f]]; 
    [cell.textLabel setTextColor:[UIColor whiteColor]]; 
    [cell setBackgroundColor:[[UIColor alloc] initWithRed:117.0/255.0 green:118.0/255.0 blue:121.0/255.0 alpha:1.0]]; 
    [self.tableView setBackgroundView:nil]; 
    [self.tableView setBackgroundView:[[UIView alloc] init]]; 
    [self.view setBackgroundColor:[[UIColor alloc] initWithRed:167.0/255.0 green:169.0/255.0 blue:172.0/255.0 alpha:1.0]]; 
    UIImageView *cellImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"[email protected]"]]; 
    [cell addSubview:cellImage]; 
    [cellImage setFrame:CGRectMake(0, 0, 26, 24)]; 
    [cellImage setCenter:CGPointMake(cell.frame.size.width - 30, cell.frame.size.height/2)]; 

    return cell; 
} 
+0

不會問相關的,但爲什麼:[self.tableView setBackgroundView:無]。 [self.tableView setBackgroundView:[[UIView alloc] init]]; [self.view setBackgroundColor:[[UIColor alloc] initWithRed:167.0/255.0 green:169.0/255.0 blue:172.0/255.0 alpha:1.0]];在你的cellforrow委託方法中?它不建議在那裏 – 2012-08-09 11:47:22

+1

爲什麼る加入cellImage細胞作爲默認的單元格有imgView u可以使用這樣cell.imgView.image = yourImage – 2012-08-09 11:49:29

+0

如果u要顯示在單元格,然後單擊使用單元格的附件disclosureIndicator和刪除您的ImageView – 2012-08-09 11:53:02

回答

0

使用下面的代碼修正:

UIImageView *cellImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"[email protected]"]]; 
[cell setAccessoryView:cellImage]; 
0

您面臨的問題是由於爲cell重複使用了標識符。 另外,您每次在執行cellForRowAtIndexPath時都會創建Imageview。

我會在這裏爲您推薦兩種解決方案。

1日選項: -只需更換驗證碼:

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 

這是不是當你在處理動態畫面適當的方式,意味着你是從服務器和數據內容下載縮略圖較多,如果是小而靜態的數據,那麼對你來說就沒問題。

第二選項: -只使用這個格式化的代碼,這將不會創建imageview每次只是使用對象從當前單元格與標籤。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 


    static NSString *CellIdentifier = @"CellIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     UIImageView *cellImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 26, 24)]; 
     cellImage.tag = 88; 
     [cell addSubview:cellImage]; 
     [cellImage setCenter:CGPointMake(cell.frame.size.width - 30, cell.frame.size.height/2)]; 

    } 
    [cell.textLabel setText:[items objectAtIndex:indexPath.row]]; 
    [cell.textLabel setFont:[UIFont fontWithName:@"Helvetica" size:15.0f]]; 
    [cell.textLabel setTextColor:[UIColor whiteColor]]; 
    [cell setBackgroundColor:[[UIColor alloc] initWithRed:117.0/255.0 green:118.0/255.0 blue:121.0/255.0 alpha:1.0]]; 
    [self.tableView setBackgroundView:nil]; 
    [self.tableView setBackgroundView:[[UIView alloc] init]]; 
    [self.view setBackgroundColor:[[UIColor alloc] initWithRed:167.0/255.0 green:169.0/255.0 blue:172.0/255.0 alpha:1.0]]; 
    UIImageView *cellImage = (UIImageView *)[cell viewWithtag:88]; 
    cellImage.image = [UIImage imageNamed:@"[email protected]"]; 

} 
+0

抱歉,我不知道如何格式化代碼,這就是爲什麼它看起來很亂。 – 2012-08-09 12:37:21

+0

使用{}格式化代碼刪除1),2)編號也可以,這可以混淆編輯器 – CSmith 2012-08-09 12:42:24

0

正如許多評論所捕獲,也有一些問題:

設置您在您的viewDidLoad方法視圖的tableView背景(或通過您的XIB),而不是在的cellForRowAtIndexPath

使用cell.imageView屬性設置左側圖標,或者,或者,您也可以使用[cell.contentView addSubview:cellImage]構建自定義單元格。如果你使用後者,你也應該像UILabel一樣添加textLabel。

要設置的tableview單元格的背景,使用此委託方法:

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
[cell setBackgroundColor:[[UIColor alloc] initWithRed:117.0/255.0 green:118.0/255.0 blue:121.0/255.0 alpha:1.0]]; 
} 

但爲了提高效率,你應該在你的ViewController一次實例化這個的UIColor並將其設置爲[電池setBackgroundColor:self.cellBackground]。

1
[self.tableView setBackgroundView:nil]; 
[self.tableView setBackgroundView:[[UIView alloc] init]]; 

線不屬於該方法。每次爲每個單元格滾動時,都會調用這些方法。在創建表視圖後,將它們放入viewDidLoad方法中。

UIImageView *cellImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"[email protected]"]]; 
[cell addSubview:cellImage]; 

這些線是真的很BAD,並可能導致內存泄漏。

每次滾動表格視圖時,都會將圖像視圖添加到單元格中。表視圖重用了單元格。這意味着當單元格被重用時,可以爲單元格添加一個新的圖像視圖。在您滾動多次後,您在每個單元格上都會有x次imageview,除非您將它們移到代碼中的某處。其他點是,如果你想添加一些子視圖到一個單元格(例如,在單元格中的init方法),然後將其添加到細胞contenview這樣

[cell.contentView addSubview:someView]; 

否則你的子視圖可能重疊附件意見。當你的單元格被調整大小時,你可能會遇到問題。

我建議你閱讀從以下鏈接指南: http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW1

相關問題