gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:36:52 25 4
gpt4 key购买 nike

我暂时以编程方式向 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com