How to add images to UITableView cell?

后端 未结 5 1370
暖寄归人
暖寄归人 2021-02-09 16:57

I temporarly added some values to the UITableViewCell programmatically. But I need to add images to each cell. How can I do that?

Here is my code. .h file

5条回答
  •  独厮守ぢ
    2021-02-09 17:14

    you adding it right, but you fogot one thing:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
        if (cell == nil) { cell = [[[UITableViewCell alloc]
                                    initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
        }
        NSUInteger row = [indexPath row]; cell.textLabel.text = [listData objectAtIndex:row];
    
        UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
        imv.image=[UIImage imageNamed:@"user.jpg"];
        [cell.contentView addSubview:imv];
        [imv release];
    
        //you fogot this:
        return cell;
         }
    

提交回复
热议问题