UITableViewCell repeating problem
Posted
by cannyboy
on Stack Overflow
See other posts from Stack Overflow
or by cannyboy
Published on 2010-06-17T12:19:34Z
Indexed on
2010/06/17
12:23 UTC
Read the original article
Hit count: 350
I have a UItableview with cells. Some cells have uilabels and some have uibuttons. The UIbuttons are created whenever the first character in an array is "^". However, the uibuttons repeat when i scroll down (appearing over the uilabel).. and then multiply over the uilabels when I scroll up. Any clues why?
My voluminous code is below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
const NSInteger LABEL_TAG = 1001;
UILabel *label;
UIButton *linkButton;
//NSString *linkString;
static NSString *CellIdentifier;
UITableViewCell *cell;
CellIdentifier = @"TableCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
label = [[UILabel alloc] initWithFrame:CGRectZero];
[cell.contentView addSubview:label];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:FONT_SIZE];
[label setNumberOfLines:0];
//[label setBackgroundColor:[UIColor redColor]];
[label setTag:LABEL_TAG];
NSString *firstChar = [[paragraphs objectAtIndex:indexPath.row] substringToIndex:1];
NSLog(@"firstChar %@", firstChar);
NSLog(@"before comparison");
if ([firstChar isEqualToString:@"^"])
{
// not called
NSLog(@"BUTTON");
//[label setFont:[UIFont boldSystemFontOfSize:FONT_SIZE]];
linkButton = [UIButton buttonWithType:UIButtonTypeCustom];
linkButton.frame = CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, 280, 30);
[cell.contentView addSubview:linkButton];
NSString *myString = [paragraphs objectAtIndex:indexPath.row];
NSArray *myArray = [myString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"*"]];
NSString *noHash = [myArray objectAtIndex:1];
[linkButton setBackgroundImage:[UIImage imageNamed:@"linkButton.png"] forState:UIControlStateNormal];
linkButton.adjustsImageWhenHighlighted = YES;
[linkButton setTitle:noHash forState:UIControlStateNormal];
linkButton.titleLabel.font = [UIFont systemFontOfSize:FONT_SIZE];
[linkButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[linkButton setTag:indexPath.row];
[linkButton addTarget:self
action:@selector(openSafari:)
forControlEvents:UIControlEventTouchUpInside];
//size = [noAsterisks sizeWithFont:[UIFont boldSystemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
[label setBackgroundColor:[UIColor clearColor]];
[label setText:@""];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
} else {
label = (UILabel *)[cell viewWithTag:LABEL_TAG];
NSString *firstChar = [[paragraphs objectAtIndex:indexPath.row] substringToIndex:1];
NSLog(@"firstChar %@", firstChar);
NSLog(@"before comparison");
if ([firstChar isEqualToString:@"^"])
{
NSLog(@"cell not nil, reusing linkButton");
linkButton = (UIButton *)[cell viewWithTag:indexPath.row];
}
}
if (!label) label = (UILabel*)[cell viewWithTag:LABEL_TAG];
NSString *textString = [paragraphs objectAtIndex:indexPath.row];
NSString *noAsterisks = [textString stringByReplacingOccurrencesOfString:@"*" withString:@""] ;
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size;
NSString *firstChar = [[paragraphs objectAtIndex:indexPath.row] substringToIndex:1];
//NSLog(@"firstChar %@", firstChar);
if ([firstChar isEqualToString:@"^"])
{
NSLog(@"BUTTON2");
if (!linkButton) linkButton = (UIButton*)[cell viewWithTag:indexPath.row];
linkButton = [UIButton buttonWithType:UIButtonTypeCustom];
linkButton.frame = CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, 280, 30);
[cell.contentView addSubview:linkButton];
NSString *myString = [paragraphs objectAtIndex:indexPath.row];
NSArray *myArray = [myString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"*"]];
NSString *noHash = [myArray objectAtIndex:1];
[linkButton setBackgroundImage:[UIImage imageNamed:@"linkButton.png"] forState:UIControlStateNormal];
linkButton.adjustsImageWhenHighlighted = YES;
[linkButton setTitle:noHash forState:UIControlStateNormal];
linkButton.titleLabel.font = [UIFont systemFontOfSize:FONT_SIZE];
[linkButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[linkButton setTag:indexPath.row];
[linkButton addTarget:self
action:@selector(openSafari:)
forControlEvents:UIControlEventTouchUpInside];
size = [noAsterisks sizeWithFont:[UIFont boldSystemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
[label setBackgroundColor:[UIColor clearColor]];
[label setText:@""];
} else if ([firstChar isEqualToString:@"*"])
{
size = [noAsterisks sizeWithFont:[UIFont boldSystemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
[label setFont:[UIFont boldSystemFontOfSize:FONT_SIZE]];
[label setText:noAsterisks];
NSLog(@"bold");
} else {
size = [noAsterisks sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setText:noAsterisks];
}
[label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 20.0f))];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
© Stack Overflow or respective owner