Ian*_*ell 19 iphone objective-c uitableview
我正在iPhone上建立一个小小的Twitter小客户端.当然,我在UITableView中显示推文,它们当然有不同的长度.我根据文字动态地改变单元格的高度:
- (CGFloat)heightForTweetCellWithString:(NSString *)text {
CGFloat height = Buffer + [text sizeWithFont:Font constrainedToSize:Size lineBreakMode:LineBreakMode].height;
return MAX(height, MinHeight);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *text = // get tweet text for this indexpath
return [self heightForTweetCellWithString:text];
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用PragProg书中的算法显示实际的推文单元格:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"TweetCell";
TweetCell *cell = (TweetCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [self createNewTweetCellFromNib];
}
cell.tweet.text = // tweet text
// set other labels, etc
return cell;
}
Run Code Online (Sandbox Code Playgroud)
当我启动时,所有可见的推文显示都很好.然而,当我向下滚动时,下面的推文已经完全拼凑起来 - 看起来一旦一个单元格从屏幕上滚动,它上面的单元格的单元格高度就会大于它应该的大小,并且模糊了部分它下面的细胞.当单元格到达视图的顶部时,它会自行重置并正确渲染.向上滚动没有任何困难.
这是一个视频,显示了这一点:http: //screencast.com/t/rqwD9tpdltd
我已经尝试了很多:在创建时调整单元格的框架,为不同高度的单元格使用不同的标识符(即[NSString stringWithFormat:@"Identifier%d", rowHeight]),在Interface Builder中更改属性...
如果还有其他可以发布的代码段,请告诉我.在此先感谢您的帮助!
| 归档时间: |
|
| 查看次数: |
17222 次 |
| 最近记录: |