我有一个应该显示新闻的UITableView。 我确实设置了2个自定义单元格 - >一个人有没有图像的新闻,另一个人有图像新闻。 所以细胞需要不同的高度。在故事板中,我创建了这两个单元格,它们每个都有一个自定义高度(280个带图像,130个没有)。
出于测试目的,我希望第一个和第三个单元格成为新闻单元+图像。 那就是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary * news = [postArray objectAtIndex:indexPath.row]; //PostArray is the array with the news
NSDictionary *authorData = [news valueForKey:@"author"];
NSString *postTitle = [self decodedString:[news valueForKey:@"title"]];
//Decode = change html chars
NSString *postExcerpt = [self decodedString:[news valueForKey:@"excerpt"]];
NSString *postComments = [news valueForKey:@"comment_count"];
//NSString *postID = [news valueForKey:@"id"];
NSString *authorFirstName = [self decodedString:[authorData valueForKey:@"first_name"]];
NSString *authorLastName = [self decodedString:[authorData valueForKey:@"last_name"]];
NSString *authorNickName = [self decodedString:[authorData valueForKey:@"nickname"]];
if (indexPath.row == 0 || indexPath.row == 2){
NewsCellImage *cell = (NewsCellImage *)[tableView dequeueReusableCellWithIdentifier:@"NewsCellImage"];
if (cell == nil) {
cell = [[NewsCellImage alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"NewsCellImage"];
}
// Set up the cell
cell.postTitle.text = postTitle;
cell.postExcerpt.text = postExcerpt;
cell.postCommentsCount.text = [NSString stringWithFormat:@"%i",indexPath.row];
//Check if names are empty
if ([authorFirstName length] == 0 && [authorLastName length] == 0){
cell.postMeta.text = [NSString stringWithFormat:@"%@ / %@",authorNickName,[news valueForKey:@"date"]];
} else {
cell.postMeta.text = [NSString stringWithFormat:@"%@ %@ / %@",authorFirstName,authorLastName,[news valueForKey:@"date"]];
}
//Set Image
NSDictionary *imageData = [news valueForKey:@"thumbnail_images"];
NSDictionary *imageDataFull = [imageData valueForKey:@"large"];
NSString *postImage = [imageDataFull valueForKey:@"url"];
[cell.postImage setImageWithURL:[NSURL URLWithString:postImage]
placeholderImage:[UIImage imageNamed:@"menu"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
if (error){
NSLog(@"Error bei Bild laden: %@",postTitle);
}
} usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
return cell;
} else {
NewsCell *cell = (NewsCell *)[tableView dequeueReusableCellWithIdentifier:@"NewsCell"];
if (cell == nil) {
cell = [[NewsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"NewsCell"];
}
// Set up the cell
cell.postTitle.text = postTitle;
cell.postExcerpt.text = postExcerpt;
cell.postCommentsCount.text = [NSString stringWithFormat:@"%@",postComments];
//Check if names are empty
if ([authorFirstName length] == 0 && [authorLastName length] == 0){
cell.postMeta.text = [NSString stringWithFormat:@"%@ / %@",authorNickName,[news valueForKey:@"date"]];
} else {
cell.postMeta.text = [NSString stringWithFormat:@"%@ %@ / %@",authorFirstName,authorLastName,[news valueForKey:@"date"]];
}
return cell;
}
}
代码确实有效,这就是启动应用后我可以看到的内容: http://cl.ly/image/320K1y081T3Z
但是当我向下滚动到下一个带有Image的单元格时出错了: http://cl.ly/image/2M2O1X3R2T13
我的代码出了问题,或者我需要添加一些内容 - 但我不知道采取哪种方式。
也许它与
有关 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexpath{ }
但我真的不知道。我很感激帮助。 TY
答案 0 :(得分:1)
试试这个方法。
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexpath{
if (indexPath.row == 0 || indexPath.row == 2){
return newsWithImageHeight;
}
else return newsHeight;
}
或者您可以在界面建设者中查看单元格的自动布局设置
答案 1 :(得分:0)
你是怎么做的:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexpath{ }
那么呢?
我认为你需要这样的东西:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexpath{
if (indexpath.row == 0 || indexpath.row == 2) {
return 280;
}
return 130;
}