8

いくつかのタスクを表示しているUITableviewことがあり、各行にはタスクを完了したかどうかをマークするチェックボックスがあります。

ユーザーがチェックボックスをタップするとチェックマークを切り替え、ユーザーが行をタップすると詳細ビューに切り替えたいと考えています。後者は使用するだけで簡単です

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

accessoryviewただし、選択領域を分離し、が選択されている場合はチェックボックスのみを切り替え、セルの残りの部分が選択されている場合にのみ詳細ビューを表示したいと思いました。UIbuttonの中にを追加するaccessoryviewと、ユーザーは行を選択しUIButton、チェックボックスを押したいだけのときに選択します。

また、ユーザーが?に沿ってドラッグしてテーブルビューをスクロールしている場合はどうなりますaccessoryviewか?UIButtonこれにより、TouchUpでアクションがトリガーされませんか?

誰かがこれを行う方法について何かアイデアがありますか?御時間ありがとうございます!

4

1 に答える 1

17

このデリゲートメソッド内でアクセサリタップを管理するのはどうですか。

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

編集:

メソッドに応答するカスタムaccessoryViewに対して、このようなことを行うことができますaccessoryButtonTappedForRowWithIndexPath:

cellForRowAtIndexPath:方法で-

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;
[button setBackgroundImage:image forState:UIControlStateNormal];

[button addTarget:self action:@selector(checkButtonTapped:event:)  forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;

- (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];
}
于 2012-08-30T03:09:40.823 に答える