单击按钮时类型降级

时间:2009-12-10 23:50:05

标签: iphone

我在contentView中有一个带有自定义UIButton和UILable的UITableViewCell。该按钮是一个复选框,可在已检查和未检查状态之间切换,每个状态都有不同的图像。如果我多次单击该按钮,UILable中的字符会在视觉上降低,每次单击都会稍微降低一点。我在标签中使用了Helvetica-Bold。

如果我弹出视图控制器然后导航回到表中,则所有内容都已修复。

想法?

JK

4 个答案:

答案 0 :(得分:2)

我认为发生的事情是,每次回收单元格时,都会添加视图。查看UICatalog示例代码中的ControlsViewController.m,注释“正在回收单元格,删除旧的嵌入式控件”应该有帮助......

答案 1 :(得分:0)

您是否负责使用主线程更改按钮的图像? 不这样做可能会在图形元素的再现中产生副作用。

使用performSelectorOnMainThread实例上的UIImageView更改其UIImage

答案 2 :(得分:0)

以下是相关代码,按钮和标签设置在 -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;
}

按钮选择器:

-(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");
}

}

在按钮单击后重新加载表数据,因为managedObjectContext已更改:

// 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];

}

答案 3 :(得分:0)

通过subClassing UITableViewCell而不是仅仅将UILabel插入contentView来修复我的问题