将图像添加到左侧的表格视图中

时间:2015-02-19 06:30:09

标签: ios uitableview

我创建了一个表格单元格,我希望在左侧添加图像。将为我创建的所有单元格提供图像。

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

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

    if (cell==nil) {
        cell=[[UITableViewCell alloc]init];
    }

     if([indexPath row] == 0){
        cell.textLabel.text=@"mano";
        cell.backgroundColor = [UIColor redColor];

    }
    if([indexPath row] == 1){
        cell.textLabel.text=@"happy";
        cell.backgroundColor=[UIColor whiteColor];
    }
    if([indexPath row] == 2){
        cell.textLabel.text=@"welcome";
        cell.backgroundColor=[UIColor grayColor];
    }
     if([indexPath row] == 3)
     {
         cell.textLabel.text=@"enjoy";
         cell.backgroundColor=[UIColor magentaColor];
     }
        return cell;
}

4 个答案:

答案 0 :(得分:3)

放入cellforrowatindexpath。

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

答案 1 :(得分:2)

cellForRowAtIndexPath 方法

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

然后

NSString *file = [[NSBundle mainBundle] pathForResource:[self.photos objectAtIndex:self.currentIndex] ofType:nil];

cell.imageView.image = [UIImage imageWithContentsOfFile:file]

代替 [UiImage imageNamed:@""] 使用 [UIImage imageWithContentsOfFile:]

因为ImageNamed需要太多内存(请检查此链接: - http://www.jorambarrez.be/blog/2012/04/19/ios-imagenamed-vs-imagewithcontentsoffile/)。

希望这会对你有所帮助。

答案 2 :(得分:0)

使用此代码

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

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

if (cell==nil) {
    cell=[[UITableViewCell alloc]init];
}



if([indexPath row] == 0){
    cell.textLabel.text=@"mano";
    cell.backgroundColor = [UIColor redColor];
    cell.imageView.image = [UIImage imageNamed:@"arrow0.png"];

}
if([indexPath row] == 1){
    cell.textLabel.text=@"happy";
    cell.backgroundColor=[UIColor whiteColor];
    cell.imageView.image = [UIImage imageNamed:@"arrow1.png"];

}
if([indexPath row] == 2){
    cell.textLabel.text=@"welcome";
    cell.backgroundColor=[UIColor grayColor];
    cell.imageView.image = [UIImage imageNamed:@"arrow2.png"];

}
 if([indexPath row] == 3)
 {
     cell.textLabel.text=@"enjoy";
     cell.backgroundColor=[UIColor magentaColor];
cell.imageView.image = [UIImage imageNamed:@"arrow3.png"];
 }
    return cell;
}

imageView是UIImageView的出口

答案 3 :(得分:0)

首先要在tableview中显示图像,不需要使用自定义单元格。首先让你的tableviewcell变为基本的。分配tableviewcell重用标识符。为tableviewcell写这段代码

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

       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

     if (cell == nil) {
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault    reuseIdentifier:@"MyIdentifier"];

    }

             cell.imageView.image = [UIImage imageNamed:@"image.png"];
             cell.backgroundColor=[UIColor magentaColor];

            return cell;

}

If you have used storyboard then no need to check if cell is nil. It intializes cell automatically when dequeing is not possible. Please check this tutorial.

http://www.raywenderlich.com/50308/storyboards-tutorial-in-ios-7-part-1

相关问题