tags:

views:

81

answers:

4

I have a UITableViewCell with a custom UIButton and UILable in the contentView. The button is a checkbox and toggles between a checked and unchecked state, with different images for each state. If I click the button multiple times the characters in the UILable degrade visually, a little more with each click. Im using Helvetica-Bold in the label.

If I pop the view controller and then navigateo back into the table, all is fixed.

Ideas?

jk

A: 

Did you take care of changing the image of the button using the main Thread ? Not doing this may create side effects in the rendition of graphic elements.

Use performSelectorOnMainThread on your UIImageView instance to change its UIImage.

yonel
A: 

Here is the code in question, the button and label are set up in the -tableView:cellForRowAtIndexPath: method

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

// Get the managedObject
NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath];

//Get the related Item object
Item *item  = [managedObject valueForKey:@"item"];


static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Set up the cell...

// Get the current status and configure the checkbox button
NSNumber *status = (NSNumber *)[managedObject valueForKey:@"status"];
NSString *imageFileName = [NSString stringWithString:@""];

if ([status intValue] == 1) {
 NSLog(@"status = 1");
 imageFileName = [imageFileName stringByAppendingString:@"checkbox-checked.png"];
} else {
 NSLog(@"status = 0");
 imageFileName = [imageFileName stringByAppendingString:@"checkbox.png"];
}

UIImage *checkBoxImage = [UIImage imageNamed:imageFileName];

CGRect frame = CGRectMake(10.0f, 15.0f, 16.0f, 16.0f);
UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] initWithFrame:frame];
[button addTarget:self action:@selector(setStatusForEntry: event:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:checkBoxImage forState:UIControlStateNormal];
[cell.contentView addSubview:button];

// Add the cell's custom text label
frame = CGRectMake(40.0f, 10.0f, 200.0f, 30.0f);

UILabel *cellLabel = [[UILabel alloc] initWithFrame:frame];
cellLabel.textColor = [UIColor blackColor];
cellLabel.backgroundColor = [UIColor clearColor];
cellLabel.textAlignment = UITextAlignmentLeft;
cellLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20.0f];
cellLabel.text = item.itemName;
[cell.contentView addSubview:cellLabel];

[cellLabel release];

return cell;
}

The button selector:

-(void)setStatusForEntry:(id)sender event:(id)event {
NSLog(@"setting status");

NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];

if (indexPath != nil) {
 NSLog (@"IndexPath from touches = %@", indexPath);
}

NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath];

NSNumber *status = (NSNumber *)[managedObject valueForKey:@"status"];
 [status intValue] == 0 ? [managedObject setValue:[NSNumber numberWithInt:1] forKey:@"status"] : [managedObject setValue:[NSNumber numberWithInt:0] forKey:@"status"];

NSLog (@"Saving new values");

NSError *error = nil;
if (![managedObjectContext save:&error]) {
 // Handle the error...
 NSLog (@"Saving error");
}

}

The table data is reloaded after a button click because the managedObjectContext has changed:

// NSFetchedResultsControllerDelegate method to notify the delegate that all section and object changes have been processed. 
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {

NSLog(@"updating table due to changed data model");

[self.tableView reloadData];

}
Alpinista
+2  A: 

I think what's going on is that each time you recycle the cell, you're adding views. Look at ControlsViewController.m in the UICatalog sample code, the comment "the cell is being recycled, remove old embedded controls" should help...

David Dunham
Your are right. The UILable should only be added if the cell is nil. I fixed my problem by subClassing the UITableCell.Jk
Alpinista
A: 

Fixed my problem by subClassing UITableViewCell instead of just inserting a UILabel into the contentView

Alpinista