2011-09-13 15 views
0

TableViewを追加する必要があります。tableviewのアイテムをいくつかクリックして、NSMutable dictionaryなどに保存する必要があります。テーブルビューから複数のアイテムを選択 - 初心者

私はこのためにNSMutable Dictionaryを使用する必要があることを知っています。しかし、私はこれを行う方法を理解していない。

誰かが良いチュートリアルを指摘したり、私のためのサンプルコードを提供してください。

答えて

0

これには代理メソッドを使用する必要があります。
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
最初に確認してくださいあなたのテーブルビューはデリゲートの実装をうまく(delegatedatasource)、その後
に設定されていることを確認します。

選択した行インデックスには、[indexPath row]でアクセスします。
この値を保存することができます。

0

これは約Cocoa With LoveのMatt Gallagher氏の投稿でしたが、あなたは明るいかもしれません。

少し古いですが、原則は同じです。それは本当にあなたのデータモデルに依存

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSMutableDictionary *dict = [NSMutableDictionary alloc] init]; 
    [dict setObject:[NSString stringWithFormat:"%@", cell.Label.text] forKey:[NSString stringWithFormat:"Key%d", indexPath.row]]; 
} 
0

あなたは、この方法ではNSMutableDictionaryを使用することができます。 まず、NSMutableDictionaryではなくNSMutableArrayが必要です。

//declare an NSMutableArray in the interface 
@interface Class : SuperClass { 
NSMutableArray *_arrayWithMySelectedItems; 
} 

//in one of your init/preparation methods alloc and initialize your mutable array; 

- (void)viewDidLoad { 
[super viewDidLoad]; 

_arrayWithMySelectedItems = [NSMutableArray alloc] init]; 
} 

//now before you forget it add release in your dealloc method 

- (void)dealloc { 
[_arrayWithMySelectedItems release]; 

[super dealloc]; 
} 

//add this following code to your didSelect Method part of tableView's delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
//if you have only one category, you will probably have something like this 
[_arrayWithMySelectItems addObject:[dataModelArray objectAtIndex:indexPath.row]; 
[tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 
0

次いで=その文字列を与えるテーブルビューのdidselect列中の文字列を宣言[あなたの移入配列objectAtIndex:indexpath.row]。それをあなたの希望に応じて辞書またはnsmutable配列に追加します。