2011-07-01 118 views
0

我在設置UIImageView或UIView對iPhone上分組表格的單元格時遇到問題。將UIImageView或UIView添加到分組表格的單元格

爲了這個,我用這個代碼

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 
    NSArray *listData =[self.tableContents objectForKey:[self.sotreKeys objectAtIndex:[indexPath section]]]; 


    UITableViewCell * cell = [tableView 
           dequeueReusableCellWithIdentifier:SimpleTableIdentifier]; 

    if(cell == nil) { 

     cell = [[[UITableViewCell alloc] 
       initWithStyle:UITableViewCellStyleDefault 
       reuseIdentifier:SimpleTableIdentifier] autorelease]; 


    } 
    if(indexPath.section == 1 && indexPath.row ==1) 
    { 
     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(240, 14, 40, 40)]; 
    imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"Back.png",nil]]; 
    [cell.contentView addSubview:imageView]; 
    [imageView release]; 
    } 

    NSUInteger row = [indexPath row]; 
    cell.textLabel.text = [listData objectAtIndex:row]; 

    return cell; 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

} 

但它不是放置在細胞。當我選擇只顯示那一行的行時,會出現什麼問題?

有沒有人有任何想法是什麼問題。我認爲圖像被細胞覆蓋。

在此先感謝

+0

仔細檢查 「Back.png」 確實存在於您的項目資源 –

+0

此外,使用[ImageView的setBackgroundColor:[的UIColor redColor];這實際上將幫助你看看莫名其妙圖像是不可見由於細胞/表背景顏色 –

+0

@joe是你有任何想法在添加圖像onthe細胞我在我的應用程序分組表視圖是問題。 – Ajay

回答

0

嘗試使

cell.accessoryType = UITableViewCellAccessoryNone; 

看來你是在附件視圖中添加圖像。

如果是的話,可以添加cell.accessoryView = imageView;

+1

的accessoryType是沒有默認,但認爲是正確的阿賈伊可以的ImageView設置爲accessoryView的。 – Joe

0

我確實對可能發生的事情有一些想法。

您正在向contentView添加圖像視圖,然後您正在設置textLabel的文本,它是contentView的一部分,可能位於圖像的頂部。試試看看你是否至少可以在左側看到你的圖片。

cell.imageView.image = [UIImage imageNamed:@"Back.png"]; 

現在,當你出列,你需要記住,你添加的所有視圖創建的細胞最後一次仍然存在,所以你可以滾動您將只是在彼此頂部堆疊後退按鈕細胞。

此外,下列行[tableView deselectRowAtIndexPath:indexPath animated:YES];是死代碼,因爲它發生在return語句之後。你可能要放置在則委託調用– tableView:willDisplayCell:forRowAtIndexPath:

0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 
    NSArray *listData =[self.tableContents objectForKey:[self.sotreKeys objectAtIndex:[indexPath section]]]; 


    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier]; 
    if(cell == nil) { 

     cell = [[[UITableViewCell alloc] 
       initWithStyle:UITableViewCellStyleDefault 
       reuseIdentifier:SimpleTableIdentifier] autorelease]; 


    } 
    if(indexPath.section == 1 && indexPath.row ==1) 
    { 
     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(240, 14, 40, 40)]; 
     imageView.image = [UIImage imageNamed:@"Back.png"]; 
     [cell.contentView addSubview:imageView]; 
     [imageView release]; 
    } 

    cell.textLabel.text = [listData objectAtIndex:indexPath.row]; 

    return cell; 
} 

請確保您的圖像名爲「Back.png」而不是「back.png」,因爲手機上的iOS是關鍵敏感的。

相關問題