自定义 UITableViewCell 图像不会出现在第一行
我面临这个非常奇怪的问题,我创建了一个自定义 UITableViewCell,在其左侧有一个 UIImageView,在右侧有两个 UILabel。
当我运行该程序时,我会看到所有图像和标签出现在桌子上。
我总共需要显示 5 行
唯一的问题是图像从第 2 行开始显示并上升到第 6 行,其中标签从第 1 行显示到第 5 行。
自定义 UITableViewCell .h 文件
@interface ProductViewCell : UITableViewCell {
IBOutlet UILabel *titleLabel;
IBOutlet UILabel *detailLabel;
IBOutlet UIImageView *imageView;
}
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UILabel *detailLabel;
@property (nonatomic, retain) UIImageView *imageView;
和 UITableViewController使用这个是
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
ProductViewCell *cell = (ProductViewCell*)[tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle]
loadNibNamed:@"ProductViewCell" owner:self options:nil];
for(id currentObject in topLevelObjects){
if([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (ProductViewCell *) currentObject;
break;
}
}
}
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
cell.titleLabel.text = [dictionary objectForKey:@"name"];
cell.detailLabel.text = @"";
cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString: [dictionary objectForKey:@"imageUrl"]]]];
return cell;
}
I am facing this very weird problem, I have created a custom UITableViewCell on which I have a UIImageView on the left and on the right I have two UILabel.
When I run the program, I see all the image and the label appears on the table.
I have total of 5 rows that needs to be displayed
The only problem is the image starts displaying from the row 2 and goes upto row 6 where as the labels get displayed from row 1 to row 5.
Custom UITableViewCell .h file
@interface ProductViewCell : UITableViewCell {
IBOutlet UILabel *titleLabel;
IBOutlet UILabel *detailLabel;
IBOutlet UIImageView *imageView;
}
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UILabel *detailLabel;
@property (nonatomic, retain) UIImageView *imageView;
And the UITableViewController that uses this is
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
ProductViewCell *cell = (ProductViewCell*)[tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle]
loadNibNamed:@"ProductViewCell" owner:self options:nil];
for(id currentObject in topLevelObjects){
if([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (ProductViewCell *) currentObject;
break;
}
}
}
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
cell.titleLabel.text = [dictionary objectForKey:@"name"];
cell.detailLabel.text = @"";
cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString: [dictionary objectForKey:@"imageUrl"]]]];
return cell;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
中执行此操作解决了问题
我通过在 tableViewController -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
返回120.0; //返回浮点数,该浮点数将用于指定行索引处的单元格行高
}
I solved the problem by doing this in my tableViewController
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 120.0; //returns floating point which will be used for a cell row height at specified row index
}
也许这与 imageView 隐藏 UITableViewCell 定义有关。我认为只有在 tableView:numberOfRowsInSection: 返回 6 时才会显示第 6 行。它也可能是一些愚蠢的东西,比如 tableDataSource 的内容;)
Perhaps it has something to do with the fact that imageView shadows the UITableViewCell definition. I think that row 6 would be displayed only if your tableView:numberOfRowsInSection: returns 6. It also might be something stupid, like the contents of the tableDataSource ;)