Why does this button on my UITableView cell repaint itself?
I have a button on my tableview cell.
But after scrolling the buttons look like this:It looks like there is sometimes one button above the other.
How can I solve this problem?That's my code for the button in cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Chapter *chapter = nil ;
UITableViewCell *cell = nil;
NSUInteger row = [indexPath row];
switch (indexPath.section) {
case 0:
chapter = [[einfuerung objectAtIndex:row]retain];
break;
case 1:
chapter = [[vertiefung objectAtIndex:row]retain];
break;
case 2:
chapter = [[spezial objectAtIndex:row]retain];
break;
}
if ([self filesFromFolderWithChapter:chapter] > 1)
{
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SimpleTableIdentifier] autorelease];
}
}
else {
static NSString *ButtonTableIdentifier = @"ButtonTableIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier: ButtonTableIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ButtonTableIdentifier] autorelease];
}
}
NSString *firstString;
NSString *lastString;
switch (indexPath.section) {
case 0:
chapter = [[einfuerung objectAtIndex:row]retain];
[dataInstance extractFromString: [chapter title]: &firstString: &lastString];
cell.textLabel.text = firstString;
cell.detailTextLabel.text = lastString;
cell.backgroundColor = [UIColor whiteColor];
break;
case 1:
chapter = [[vertiefung objectAtIndex:row]retain];
[dataInstance extractFromString: [chapter title]: &firstString: &lastString];
cell.textLabel.text = firstString;
cell.detailTextLabel.text = lastString;
cell.backgroundColor = [UIColor whiteColor];
break;
case 2:
chapter = [[spezial objectAtIndex:row]retain];
[dataInstance extractFromString: [chapter title]: &firstString: &lastString];
cell.textLabel.text = firstString;
cell.detailTextLabel.text = lastString;
cell.backgroundColor = [UIColor whiteColor];
break;
}
if ([self filesFromFolderWithChapter:chapter] > 1)
{
//VersionButton *button = [VersionButton buttonWithType:UIButtonTypeRoundedRect];
VersionButton *button = [[VersionButton alloc]initWithFrame:CGRectMake(500, 15, 150, 30) andTitle:@"ältere Versionen"];
button.chapter = chapter;
[button addTarget:self action:开发者_JAVA技巧@selector(versionTapped:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
[button release];
}
[chapter release];
return cell;
}
First of all it would be helpful to see your entire cellForRowAtIndexPath function.
It looks like you are not accounting for cell reuse correctly. You should only add a button to your cell if you are creating a cell from scratch and not if a cell is being reused. If a cell is being reused you should have a way to access the button it already has and make any changes necessary to it.
are you calling setNeedsDisplay somewhere else that would cause it to call the drawRect?
精彩评论