如何在iOS中以编程方式在UITableViewCells中添加图像

时间:2014-03-12 10:43:59

标签: ios iphone objective-c uitableview

我需要以编程方式在UITableViewCells中添加不同的图像。我怎样才能做到这一点。我正在尝试一些代码。但图像不会显示在UITableViewCells中。 这是我的代码。

-(void)viewDidLoad
{
    arrImages=[[NSMutableArray alloc]initWithObjects:@"image.png",@"image2.png",@"image3.png",@"image4.png",@"image5.png",@"image6.png",@"image7.png",@"image8.png" nil];
}

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

    static NSString *identifier=@"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) 

    {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"];

    }

    imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 48, 48)];

    cell.imageView.image = [UIImage imageNamed:[arrImages objectAtIndex:indexPath.row]];

    return cell;
}

3 个答案:

答案 0 :(得分:2)

这是你可能想要的:

- (void)viewDidLoad
{
    self.arrImages = [[NSMutableArray alloc] initWithObjects:@"image.png",@"image2.png",@"image3.png",@"image4.png",@"image5.png",@"image6.png",@"image7.png",@"image8.png" nil];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* CellIdentifier = @"Cell";

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]
    }

    cell.imageView.image = [UIImage imageNamed:[self.arrImages objectAtIndex:indexPath.row]];

    return cell;
}

P.S。:下次,请努力格式化源代码,解释错误,尝试了什么,想要完成什么。感谢

答案 1 :(得分:1)

在.h文件中创建NSArray的属性

@property NSArray *imgArray;

在您的.m文件中合成

@synthesize imgArray;

在viewDidLoad中写下这个

- (void)viewDidLoad
{
    [super viewDidLoad];


    // Initialize images...
    imgArray = [NSArray arrayWithObjects:@"yourFirstImageName.png", @"yourSecondImageName.png", @"yourThirdImageName.png",....., @"yourSeventhImageName.png", nil];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

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


        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 48, 48)]; // your cell's height should be greater than 48 for this.
        imgView.tag = 1;
        [cell.contentView addSubview:imgView];
        imgView = nil;

        UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];

        lbl.backgroundColor = [UIColor clearColor];
        [lbl setTag:2];
        [cell.contentView addSubview:lbl];
        lblDisch = nil;
    }

    UIImageView *_imgView = (UIImageView *)[cell.contentView viewWithTag:1];
    _imgView.image = [UIImage imageNamed:[imgArray objectAtIndex:indexPath.row]];

    UILabel *_lbl = (UILabel *)[cell.contentView viewWithTag:2];
    _lbl.text =  [arrFont objectAtIndex:indexPath.row]; // arrFont is your array
    return cell;
}

希望这会对你有所帮助。

答案 2 :(得分:-2)

在.h文件中创建一个NSMutableArray的@property。

NSMutableArray *data;

将此代码写为.m文件。

- (void)viewDidLoad
{
    data = [[NSMutableArray alloc]initWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg",@"10.jpg",@"11.jpg",@"12.jpg",@"13.jpg",nil];
    [super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cellID";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.imageView.image = [UIImage imageNamed:[data objectAtIndex:indexPath.row]];

    return cell;
}
相关问题