1

我正在创建一个基于 TableView 的应用程序。tableView 正在加载外部 XML 提要的最后 12 项。这一切都很完美。

所以现在我想创建一个额外的“保存最喜欢的项目功能”。有两种方法可以实现这一点:

1.自定义附件按钮

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 

2.(自定义)编辑tableview

if ([self.tableView isEditing])

我的问题是:您更喜欢哪个选项,您能否举例说明如何实现这一目标?

任何认真的答案将不胜感激。

谢谢您的回答。感谢马特,我用以下代码修复了它:

        NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];
    BOOL checked = [[item objectForKey:@"checked"] boolValue];
    //cell.backgroundColor = [UIColor clearColor];
    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    UIImage *image = (checked) ? [UIImage   imageNamed:@"first.png"] : [UIImage imageNamed:@"second.png"];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    button.frame = frame;
    [button setBackgroundImage:image forState:UIControlStateNormal];
    [button addTarget:self action:@selector(checkButtonTapped:event:)  forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor clearColor];
    cell.accessoryView = button;

如您所见,我现在正在使用 dataAray。我还使用了一个存储“检查布尔值”的 plist。这不能正常工作,因为:

  1. 复选标记未正确放置(根据 plist)
  2. 当 UitableView 滚动视图移动时,复选标记会随机更改。

SO I want to create an array which stores Id's of the selected items. Then iterate through the array to see if an ID is present in the array. If yes: Star if No: graystar.

Do you think this is a good solution?

4

1 回答 1

0

I very much prefer accessory button approach. Since editing mode is typically used to remove or re-oder items, the user would not usually look there or expect it to perform any other functions.

Having said that, I would not recommend using the accessory button without changing its default image. Since the default image of the accessory button usually means displaying more details about the current item, it might cause confusion if not changed to a more descriptive image. Also, optimally the image should be different if the current item is marked as a favorite (e.g. a grey star if it's not a favorite, and a golden star if it is). The code should be really straight forward:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  cell = UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Identifier"];
  if(!cell)
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Identifier"]

  UIImage *accessoryImage;
  ItemClass *item = [self.items objectAtIndex:indexPath.row];
  // you might want to cache the images instead of creating them for each cell
  if(item.favorite)
    accessoryImage = [UIImage imageNamed:@"goldenStar.png"];  
  else
    accessoryImage = [UIImage imageNamed:@"greyStar.png"];  
  cell.accessoryView = [[UIImageView alloc] initWithImage:accessoryImage];
}

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
  ItemClass *item = [self.items objectAtIndex:indexPath.row];
  item.favorite = !item.favorite;

  // update the cell with the new image and any other data you need modified
  [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

Good luck!

于 2013-03-31T02:26:41.960 回答