当offset
UITableView
dataSource
添加新帖时,我正在制作某种Facebook时间线类似应用。向[self.tableView reloadData]
我- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PostCell";
PostCell *postCell = (PostCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
Post *post = self.displayPosts[indexPath.row];
[postCell setPost:post];
return postCell;
}
- (void)updatePosts
{
[[APIClient sharedInstance] downloadPostsForPage:_currentPage withSuccessionBlock:^(NSArray *posts) {
NSLog(@"Display new posts!");
[self.displayPosts addObjectsFromArray:posts];
[self.mainTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
_isLoadingMore = NO;
} failureBlock:^(NSError *err) {
// handle error
[self handleError:err];
}];
}
添加新帖后,一切都很好,除了小滞后和有时错误的偏移(有时候+/- 150 px取决于你滚动的方向)。
代码:
@interface PostCell ()
@property (weak, nonatomic) IBOutlet PostView *postView;
@property (nonatomic, strong) Post *post;
@end
@implementation PostCell
- (void)setPost:(Post *)newPost
{
self.postView.post = newPost;
}
发布单元格:
- (void)setPost:(Post *)newPost
{
if (_post != newPost) {
_post = newPost;
}
[self layoutSubviews];
[self refreshContent];
}
- (void)layoutSubviews
{
[super layoutSubviews];
// define title attributes
NSDictionary *attributes = @{
NSForegroundColorAttributeName : [UIColor blackColor],
NSFontAttributeName : [UIFont systemFontOfSize:20.0f]};
CGFloat boundingTitleWidth = self.bounds.size.width - LEFT_TITLE_OFFSET - RIGHT_TITLE_OFFSET;
CGRect titleRect = [_post.title boundingRectWithSize:CGSizeMake(boundingTitleWidth, CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:attributes context:nil];
if (self.titleLabel == nil) {
self.titleLabel = [[UILabel alloc] init];
[self addSubview:self.titleLabel];
}
[self.titleLabel setFrame:CGRectMake(LEFT_TITLE_OFFSET, TOP_TITLE_OFFSET, titleRect.size.width, titleRect.size.height)];
[self.titleLabel setNumberOfLines:0];
[self.titleLabel sizeToFit];
// Draw the quarter image.
CGFloat imageY = TOP_TITLE_OFFSET + titleRect.size.height + TOP_IMAGE_OFFSET;
CGPoint point = CGPointMake(LEFT_IMAGE_OFFSET, imageY);
CGSize imageSize = self.post.image.size;
if (self.imageView == nil) {
self.imageView = [[UIImageView alloc] init];
[self addSubview:self.imageView];
}
[self.imageView setFrame:CGRectMake(point.x, point.y, imageSize.width, imageSize.height)];
}
- (void)refreshContent
{
[self.titleLabel setText:_post.title];
[self.imageView setImage:self.post.image];
}
后览:
{{1}}
任何帮助?
感谢。