2012-02-23 6 views
2

私のアプリケーションでは、.plistファイルのセクションを持つテーブルを作成するテーブルビューコントローラがあります。すべてのセルは、plistファイル内のすべての項目に格納され、繰り返すことができないIDパラメータに基づいているため、ユニークです。 PLISTの例として:セルのテキストデータでNSIndexPath(UITableCell用)を取得するにはどうすればよいですか?

<dict> 
<key>Section One</key> 
<dict> 
    <key>Item 0</key> 
    <dict> 
     <key>name</key> 
     <string>Some name one</string> 
     <key>picfile</key> 
     <string>take8.png</string> 
     <key>takeItemId</key> 
     <integer>101</integer> 
    </dict> 
    <key>Item 1</key> 
    <dict> 
     ... 
    </dict> 
</dict> 
<key>Section Two</key> 
<dict> 
    <key>Item 0</key> 
    <dict> 
     <key>name</key> 
     <string>Some name two</string> 
     <key>picfile</key> 
     <string>take6.png</string> 
     <key>takeItemId</key> 
     <integer>103</integer> 
    </dict> 
    <key>Item 1</key>.... 

だから私のテーブルには、テキストラベル(takeItemID)、ファイルから画像や他のテキストラベル(名前)を含む細胞で構成されています。 今、私はIDとして文字列(私はTCPソケット接続からコマンドを受け取る)をもたらすいくつかのイベントがあります。受信したID文字列を対応するテキストラベルとして含むセルで、テーブルをハイライト表示(背景を変更)する必要があります。何とかplistからNSIndexPathを取得することもOKです。

編集:cellForRowAtIndexPath::

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {  
NSUInteger section = [indexPath section]; 
NSUInteger row = [indexPath row]; 

NSString *sGroup = [sequencerGroups objectAtIndex:section]; 
NSDictionary *tSection = [sequencer objectForKey:sGroup]; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SecuencerCell"]; 

NSString *takeItemName = [[[tSection allKeys] sortedArrayUsingSelector:@selector(compare:)] objectAtIndex:row]; 
NSDictionary *takeItem = [tSection objectForKey:takeItemName]; 

UILabel *idLabel = (UILabel *)[cell viewWithTag:104]; 
idLabel.text = [NSString stringWithFormat:@"%@",[takeItem valueForKey:@"takeItemId"]]; 

UILabel *nameLabel = (UILabel *)[cell viewWithTag:102]; 
nameLabel.text = [takeItem valueForKey:@"name"]; 

UIButton *piconButton = (UIButton *)[cell viewWithTag:101]; 
UIImage *TakeImage = [UIImage imageNamed:[takeItem valueForKey:@"picfile"]]; 
[piconButton setBackgroundImage:TakeImage forState:UIControlStateNormal]; 


UIButton *resumeButton = (UIButton *)[cell viewWithTag:103];  
UIImage *resumeImage = [UIImage imageNamed:@"resume.png"]; 
[resumeButton setBackgroundImage:resumeImage forState:UIControlStateNormal]; 



return cell; 
} 

編集:ここでは以下の私のtableViewある

NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Sequencer.plist"]; 

NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path]; 
self.sequencer = dict; 
[dict release]; 

NSArray *array = [sequencer allKeys]; 

self.sequencerGroups = array; 

どうもありがとう、 マキシム

+0

TISがあなたにhttp://borkware.com/quickies/oneを助けるかもしれませんか? topic = UITableView – fibnochi

+0

'tableView:cellForRowAtIndexPath:'のコードを含めることができますか? – dasblinkenlight

答えて

3

:ここsequencerGroupsとシーケンサーから取られているところでありますrowNumberからindexPathを作成するには、次のコードを使用します。[NSIndexPath indexPathForRow:<#(NSInteger)#> inSection:<#(NSInteger)#>];

行番号が&のセクション番号になると、これによりrow-> indexPath変換が簡単になります。私はtakeItemIdから行&セクション番号を得るのですか -

だからあなたの質問はその後になります。

私はそれにアプローチする方法を考えています(しかし、plistを読み込む実行時間がどれほど重要かによって、考慮できる方法はたくさんあります)。plistを解析した直後に、私はNSDictionarytakeItemIdをIndexPathにマップします。

例:あなたはmapKeyToIndexPath辞書を持っていたら、あなたは、あなたがあなたのイベントでtakeItemIdを受信したときに正しいindexPathをつかむと、[myTableDataSource tableView:myTableView highlightCellAtIndexPath:indexPath];

+0

注 - これは「うまくいく」解決策です。生産ソリューションにとってより守る必要があります。例えばplistが作成したオブジェクトの正しいタイプを確認し、予期しないタイプを正常に処理します。 – ikuramedia

+0

ありがとうikuragames!私はこれを試す必要があります。 –

1

に渡すことができ

NSDictionary *myPlist = ... 
NSMutableDictionary *mapKeyToIndexPath = [NSMutableDictionary dictionary]; 
NSUInteger section = 0; 
NSUInteger row = 0; 
for (id sectionKey in [myPlist allKeys]) { 
    // Parse Sections First 
    NSDictionary *section = [myPlist objectForKey:sectionKey]; 
    for (id rowKey in [section allKeys]) { 
     // Parse Rows Next 
     NSDictionary *row = [section objectForKey:rowKey]; 
     NSString *takeItemId = [row objectForKey:@"takeItemId"]; 

     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row++ inSection:section]; 
     [mapKeyToIndexPath addObject:indexPath forKey:takeItemId]; 
    } 
    // Move to first row, next section 
    row = 0; 
    section ++; 
} 

私はIDを検索しますセクションと行が何であるかを判断するためにplistで。 その後、私はNSIndexPathを作成します。

//create the indexPath with the value you got from plist 
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(NSInteger) inSection:(NSInteger)]; 

//get the cell in the table myTable, and do what you want with it 
cell = [myTable cellForRowAtIndexPath:indexPath]; 

あなたはまた、法scrollToRowAtIndexPathでセルにスクロールすることができますatScrollPosition:アニメーション:

+0

フランクの「セクションと行が何であるかを決めるために、plistのIDを検索する」と正確に何を意味していますか?これは私の実際の質問です! –

関連する問題