xcode 如何使用核心数据在附件示例中获取 UITableViewCell
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4425537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to obtain UITableViewCell in Accessory sample using Core Data
提问by jon
I have an application based on CoreDataBooks. I want to toggle two images in the cell accessory button as noted in the iOS sample code, but am running into a roadblock. I cannot figure out how to obtain the UITableViewCell that is noted in the accessoryButtonTappedForRowWithIndexPath: method. The sample stored the object in the item as key-value, but I cannot figure out how to do this with my patient and core data.
我有一个基于 CoreDataBooks 的应用程序。我想按照 iOS 示例代码中的说明在单元格附件按钮中切换两个图像,但遇到了障碍。我无法弄清楚如何获取在附件按钮TappedForRowWithIndexPath: 方法中注明的 UITableViewCell。该示例将项目中的对象存储为键值,但我无法弄清楚如何使用我的患者和核心数据执行此操作。
First of all, here is the relevant code from the Accessory sample:
首先,这是附件示例中的相关代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *kCustomCellID = @"MyCellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCellID];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];
cell.textLabel.text = [item objectForKey:@"text"];
[item setObject:cell forKey:@"cell"];
BOOL checked = [[item objectForKey:@"checked"] boolValue];
UIImage *image = (checked) ? [UIImage imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame; // match the button's size with the image size
[button setBackgroundImage:image forState:UIControlStateNormal];
// set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
cell.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
return cell;
}
- (void)checkButtonTapped:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
if (indexPath != nil)
{
[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];
BOOL checked = [[item objectForKey:@"checked"] boolValue];
[item setObject:[NSNumber numberWithBool:!checked] forKey:@"checked"];
UITableViewCell *cell = [item objectForKey:@"cell"];
UIButton *button = (UIButton *)cell.accessoryView;
UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
}
I am able to set my image correctly by using a "check" property of "Y" or "N" that I can store in CoreData, as noted below:
通过使用可以存储在 CoreData 中的“Y”或“N”的“check”属性,我能够正确设置我的图像,如下所述:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
Patient *patient = [fetchedResultsController_ objectAtIndexPath:indexPath];
cell.textLabel.text = patient.name;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
UIImage *image = [UIImage imageNamed:@"unchecked.png"];
if ([patient.check isEqualToString: @"Y"] == YES)
image = [UIImage imageNamed:@"checked.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame; // match the button's size with the image size
[button setBackgroundImage:image forState:UIControlStateNormal];
// set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
cell.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
}
However, I am having problems with this method, as noted in the code:
但是,我在使用此方法时遇到了问题,如代码中所述:
- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
Patient *patient = [fetchedResultsController_ objectAtIndexPath:indexPath];
// HOW CAN I OBTAIN THE UITABLEVIEWCELL?
UITableViewCell *cell = [item objectForKey:@"cell"];
UIButton *button = (UIButton *)cell.accessoryView;
UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
}
I appreciate any assistance.
我感谢任何帮助。
AMENDMENT: Thanks to BoltClock, this all came together. Posting the final code in case anyone else wants to use this methodology.
修正:多亏了 BoltClock,这一切都汇集到了一起。发布最终代码,以防其他人想使用这种方法。
- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
Patient *patient = [fetchedResultsController_ objectAtIndexPath:indexPath];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIButton *button = (UIButton *)cell.accessoryView;
UIImage *image = [UIImage imageNamed:@"checked.png"];
// patient.check is a string value set to either "Y" or "N"
// This allows the check mark to be permanently saved
if ([patient.check isEqualToString: @"Y"] == YES) {
image = [UIImage imageNamed:@"unchecked.png"];
patient.check = @"N";
}
else {
// image defaults to checked.png
// image = [UIImage imageNamed:@"checked.png"];
patient.check = @"Y";
}
[button setBackgroundImage:image forState:UIControlStateNormal];
// Save patient.check value
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
回答by BoltClock
Use the table view's cellForRowAtIndexPath:
method and pass in the index path as the argument.
使用表视图的cellForRowAtIndexPath:
方法并传入索引路径作为参数。
- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
Patient *patient = [fetchedResultsController_ objectAtIndexPath:indexPath];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIButton *button = (UIButton *)cell.accessoryView;
UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
}