UITableView custom cell showing weird results? I have my code below. Everything was showing fine until I put enough data to go off the initial screen then it started going crazy? My custom cell is shown below.
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"customCell"; // This is identifier given in IB jason set this.
PostTableCustomCellController *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSLog(@"Cell created");
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"PostTableCustomCellController" owner:nil options:nil];
for(id currentObject in nibObjects)
{
if([currentObject isKindOfClass:[PostTableCustomCellController class]])
{
cell = (PostTableCustomCellController *)currentObject;
}
}
}
Post *post = [postsArray objectAtIndex:indexPath.row];
cell.authorName.text = post.author;
cell.deadline.text = post.deadline;
cell.description.text = post.description;
Post *myPost = [postsArray objectAtIndex:[indexPath row]];
NSString *text = myPost.description;
// Configure the cell...
// [[cell authorName] setText:@"1 day"];
// [[cell distance] setText:@"Austin, TX"];
// [[cell description] setText:@"dd" ];
// Might can remove these
UILabel *locationLabel = (UILabel *) [cell distance];
UILabel *postTextView = (UILabel *) [cell description];
//CGSize maximumLabelSize = CGSizeMake(254,88.0f);
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 88.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 35.0f);
CGFloat realHeight = height + 36.开发者_JS百科0f - (10);
CGSize expectedLabelSize = [text sizeWithFont:postTextView.font
constrainedToSize:constraint
lineBreakMode:postTextView.lineBreakMode];
CGRect newFrame = postTextView.frame;
newFrame.size.height = expectedLabelSize.height;
postTextView.frame = newFrame;
[[cell description] sizeToFit];
CGRect locationRect = locationLabel.frame; // calls the getter
locationRect.origin.y = realHeight;
/* CGFloat locRectHeight = postTextView.bounds.size.height; */
/* locationRect.origin.y = cell.bounds.size.height; */
locationLabel.frame = locationRect;
//[[cell authorName] setText:@"jgervin"];
[[cell viewForBackground] sizeToFit];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
Post *myPost = [postsArray objectAtIndex:[indexPath row]];
NSString *text = myPost.description;
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 88.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 35.0f);
return height + (CELL_CONTENT_MARGIN * 2) + 36.0f;
}
You queing up old cells as they go off screen do to your dequeReusableCell
, and the content and properties from the old cell must still be there. When you add content to a cell make sure to clear any previous properties so you can avoid these issues, or just turn off dequeReusableCell
.
It looks like a cell-reuse problem. Why dont your turn off the dequeue-cell mechanism and see if the problem goes away. If it does, take a look at the code in your custom cell - are you creating multiple copies of the field labels? It looks that way.
精彩评论