2011-07-01 117 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是关键敏感的。

相关问题