Ios 如何将图像添加到UITableView单元格?
我临时以编程方式向Ios 如何将图像添加到UITableView单元格?,ios,uitableview,Ios,Uitableview,我临时以编程方式向UITableViewCell添加了一些值。但我需要在每个单元格中添加图像。我该怎么做 这是我的密码。h文件 @interface BidalertsViewController : UIViewController <UITableViewDelegate,UITableViewDataSource> { NSArray *listData; } @property(nonatomic, retain) NSArray *listData;
UITableViewCell
添加了一些值。但我需要在每个单元格中添加图像。我该怎么做
这是我的密码。h文件
@interface BidalertsViewController : UIViewController <UITableViewDelegate,UITableViewDataSource> {
NSArray *listData;
}
@property(nonatomic, retain) NSArray *listData;
@end
代码未向单元格中添加图像。有什么问题吗?如何将自定义图像添加到单元格?您可以使用单元格的imageView属性添加图像:
cell.imageView.image = [UIImage imageNamed:@"user.jpg"];
在您的
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(nsindepath*)indepath
方法中。下面是一个示例单元格,在表格单元格中加载facebook图像
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// Leave cells empty if there's no data yet
if (nodeCount > 0)
{
// Set up the cell...
AppRecord *appRecord = [self.friendsList objectAtIndex:indexPath.row];
cell.textLabel.text = friend.name;
// Only load cached images; defer new downloads until scrolling ends
if (!appRecord.icon)
{
// set the url
appRecord.imageURLString = [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture", friend.recordID];
// request the download
if (self.tableView.dragging == NO && self.tableView.decelerating == NO)
{
[self startIconDownload:appRecord forIndexPath:indexPath];
}
// if a download is deferred or in progress, return a placeholder image
cell.imageView.image = [UIImage imageNamed:@"Placeholder.png"];
}
else
{
cell.imageView.image = appRecord.icon;
}
}
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;
}
你加对了,但你忘了一件事:
- (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;
}
//为图像视图创建一个矩形框
UIImageView *iconImage = [[UIImageView alloc] init];
iconImage.frame = CGRectMake(12,4,53.5,53.5);
// Create a label for cells
UILabel *cellName = [[UILabel alloc] initWithFrame:CGRectMake(75,18,200,20)];
cellName.font = [self fontForBodyTextStyle];
NSString *cellNameStr = [NSString stringWithFormat:@"%@",self.tableCellNames[indexPath.row]];
// Select path row tab actions
switch (indexPath.row) {
case 0:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:@"condition.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
case 1:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:@"videos.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
case 2:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:@"provider.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
case 3:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:@"info.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
default:
NSAssert(NO, @"Unhandled value in cellForRowAtIndexPath");
break;
}
谢谢VKJ,它不会工作,因为您已经有了返回单元格代码>以上;)哦在这一行的末尾,是的。那就换掉它,我刚编辑过。问题在于:您将未完成的单元格返回到tableView。我的意思是,你先返回了它,然后添加了图像,所以它是ignoredoh尼斯。谢谢你的回复。显示图像。但它显示在文本前面。文本是隐藏的。如何分割文本和图像尝试Matteo Alessani解决方案(此问题的投票结果)。如果没有帮助,您需要添加自己的标签,而不是通常的标签,就像您添加图像一样。是的,我也添加到了参考资料文件中。是的,当然。它被显示出来了。但图像显示在文本前面。文本未完全显示。在这种情况下,需要使用自定义单元格,对UITableViewCell类进行子类化。您可以按照如下教程进行操作:或搜索显示的“自定义uitableviewcell”图像。但图像显示在文本前面。文本未完全显示。它工作正常,因为textlab
的框架与UIImageView
相交。可能您需要自定义单元格。
UIImageView *iconImage = [[UIImageView alloc] init];
iconImage.frame = CGRectMake(12,4,53.5,53.5);
// Create a label for cells
UILabel *cellName = [[UILabel alloc] initWithFrame:CGRectMake(75,18,200,20)];
cellName.font = [self fontForBodyTextStyle];
NSString *cellNameStr = [NSString stringWithFormat:@"%@",self.tableCellNames[indexPath.row]];
// Select path row tab actions
switch (indexPath.row) {
case 0:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:@"condition.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
case 1:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:@"videos.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
case 2:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:@"provider.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
case 3:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:@"info.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
default:
NSAssert(NO, @"Unhandled value in cellForRowAtIndexPath");
break;
}