ios - 如何将图像添加到 UITableView 单元格?

标签 ios uitableview

我暂时以编程方式向 UITableViewCell 添加了一些值。但我需要向每个单元格添加图像。我该怎么做?

这是我的代码。 .h文件

 @interface BidalertsViewController : UIViewController       <UITableViewDelegate,UITableViewDataSource> {
    NSArray *listData;
}
@property(nonatomic, retain) NSArray *listData; 
@end

.m文件

 @synthesize listData;

- (void)viewDidLoad {
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(5,0,310,28)];
newView.backgroundColor=[UIColor whiteColor];
UITextView *mytext = [[UITextView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 25.0)];
mytext.backgroundColor = [UIColor clearColor];
mytext.textColor = [UIColor blackColor];
mytext.editable = NO;
mytext.font = [UIFont systemFontOfSize:15];
mytext.text = @"Asset Name";
[mytext release];
[newView addSubview:mytext];

[self.view addSubview:newView];
[newView release];


NSArray *array = [[NSArray alloc] initWithObjects:@"iPhone", @"iPod", @"iPad",nil];
self.listData = array;
[array release];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
     [super didReceiveMemoryWarning];
}
 - (void)dealloc {
  [listData dealloc];
  [super dealloc];
}


 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
return [self.listData count];
 }

 - (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]; 
return cell;

UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imv.image=[UIImage imageNamed:@"user.jpg"];
[cell.contentView addSubview:imv];
[imv release];
 }
@end

代码没有向单元格中添加图像。有什么问题?如何将自定义图像添加到单元格?

最佳答案

return cell; 移动到方法的末尾:

- (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];

    return cell;
}

关于ios - 如何将图像添加到 UITableView 单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8148260/

相关文章:

ios - 在使用 SFRestAPI 时一起使用部分和 tableviewcells 时出错

ios - 您是否必须使用 UITableView 而不是 UITableViewController 在 tableview 上方添加按钮?

iphone - 使用 reloadData 作为 UITableView 的保险单

iphone - UINavigationController 的通用右侧 UIBarButton

ios - 检测是否在应用程序未启动且用户未通过它们打开应用程序时收到推送通知

ios - Q : tableView created by storyboard running appears space form the top to the first cell

ios - 对于自定义 UIView() 类,不会自动调整 UITableView 标题部分的高度

ios - Twitter API - STTwitter 库 : Unhandled authentication challenge type - NSURLAuthenticationMethodOAuth2

iphone - 应用程式无法使用行动网路连线(3G)

ios - UITableView 单元格未添加到屏幕底部下方