TableView ベースのアプリケーションを作成しています。tableView は、XML フィードの外部の最後の 12 項目に読み込まれています。それはすべて完璧に機能します。
そこで、「お気に入りアイテム保存機能」を追加したいと思います。これを実現するには、次の 2 つの方法があります。
1. カスタム アクセサリ ボタン
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
2. (カスタマイズされた) テーブルビューの編集
if ([self.tableView isEditing])
私の質問は次のとおりです。どのオプションが好みですか。これを達成する方法の例を挙げていただけますか?
どんな深刻な答えでも大歓迎です。
ご回答ありがとうございます。Matt のおかげで、次のコードで修正しました。
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も使用しています。次の理由により、これは正しく機能しません。
- チェックマークが正しく配置されていません (plist によると)
- UitableView のスクロールビューが移動すると、チェックマークがランダムに変化します。
SO 選択した項目の ID を格納する配列を作成したいと思います。次に、配列を反復処理して、配列に ID が存在するかどうかを確認します。はいの場合: いいえの場合: グレースター。
これは良い解決策だと思いますか?