UItableview header flicker when we reuse the cell Identifier
I have created a cell using
static NSString *CellIdentifier = @"TweetListUserName";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]autorelease] ;
}
and used the following code to set up a section header:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView* customView = [[[UIView alloc] initWithFrame:CGRectMake(0.0, -4.0, 320.0, 67.0)]autorelease];
[customView setOpaque:YES];
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(-2.0, -4.0, 328.0, 67.0)];
[image setImage:[UIImage imageNamed:@"header-msg.png"]];
[image setOpaque:YES];
[customView addSubview:image];
UILabel *label1 =[[UILabel alloc] initWithFrame:CGRectMake(10, 5, 100, 12)];
label1.text =@"TWEETS MATCHING:";
label1.textAlignment=UITextAlignmentLeft;
[label1 setTextColor:[UIColor grayColor]];
[label1 setFont:[UIFont fontWithName:@"H开发者_JS百科elvetica" size:9]];;
[label1 setBackgroundColor:[UIColor clearColor]];
[customView addSubview:label1];
[label1 release];
UILabel *label2 =[[UILabel alloc] initWithFrame:CGRectMake(10, 6, 315, 50)];
label2.text =usernametag;
label2.textAlignment=UITextAlignmentLeft;
[label2 setTextColor:[UIColor blackColor]];
[label2 setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
[label2 setBackgroundColor:[UIColor clearColor]];
[customView addSubview:label2];
[label2 release];
return customView;
}
However, when I quickly scroll the table the header flickers in almost all cases. How can I prevent this flickering?
Hi I think you need to test if it is already set. The way you have it now it will run the above code each time you scroll, meaning it will get set quite a lot.
I have had a similar issue.
In my case the reason was within the custom table cell.
Within my custom table cell I used aUITextView
oject with its scrollEnabled
property set to NO
and the editable
property set to NO
.
The probelm was linked to scrollEnabled=NO. With scrollEnabled=YES I hat no issues.
However, as a UITextView
without editing and scrolling is not much different from a UILabel
, I replaced the UITextView
with a UILabel
setting numberOfLines
to 0.
Don't ask me why. It somply worked that way. :)
It's because you are changing the frame of the header in layoutsubviews. If you remove that, it will be fixed.
精彩评论