2012-02-23 31 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也可以。

编辑:这下面是我的TableView:的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; 
} 

编辑:这里是sequencerGroups和定序器是摘自:

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

提斯可以帮助你http://borkware.com/quickies/one? topic = UITableView – fibnochi 2012-02-23 15:02:08

+0

你可以包含你的'tableView:cellForRowAtIndexPath:'代码吗? – dasblinkenlight 2012-02-23 15:04:34

回答

3

要从rowNumber创建索引路径,您可以使用:[NSIndexPath indexPathForRow:<#(NSInteger)#> inSection:<#(NSInteger)#>];

这使得row-> indexPath转换变得很容易,一旦你有行&节号。

所以你的问题就变成了 - 我怎么从takeItemId中得到&这一行的段号。

我想我会采取的方式(但有很多方法可以考虑,但取决于装载你的plist的执行时间有多关键) - 在我解析plist后,我会构造一个NSDictionary它将takeItemId映射到IndexPath。

一个例子:

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 ++; 
} 

一旦你的mapKeyToIndexPath字典,你可以再抢正确indexPath当您在事件收到takeItemId并将它传递给[myTableDataSource tableView:myTableView highlightCellAtIndexPath:indexPath];

+0

注 - 这是一个'让你工作'的解决方案,你需要对生产解决方案采取更多防御措施。例如确认plist创建的正确类型的对象并优雅地处理意外的类型。 – ikuramedia 2012-02-23 15:19:45

+0

感谢ikuragames!我需要尝试一下。 – 2012-02-23 15:42:06

1

我会搜索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来确定什么是部分和行”Franck?这是我真正的问题! – 2012-02-23 15:44:26

相关问题