如何通过单元格的文本数据获取 NSIndexPath(对于 UITableCell)?
在我的应用程序中,我有一个表视图控制器,它使用 .plist 文件中的部分构建一个表。每个单元格都是唯一的,因为它基于为 plist 文件中的每个项目存储的 ID 参数,并且不能重复。作为 plist 的示例:
<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和sequencer的来源:
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Sequencer.plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.sequencer = dict;
[dict release];
NSArray *array = [sequencer allKeys];
self.sequencerGroups = array;
非常感谢, 格言
In my application i have a table view controller that builds a table with sections from a .plist file. Every cell is unique, as it's based on ID parameter that is stored for every item in the plist file and cannot repeat. As example of the plist:
<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>....
So my table consists of cells containing a text label (takeItemID), a picture from file and another text label (Name).
Now, i have some event that brings an ID as a string (I receive commands from tcp socket connection). And I want my table to highlight (to change a background) in those cells that contain received ID string as the corresponding text label. Getting NSIndexPath somehow from the plist would be OK as well.
Edit: here below is my 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;
}
Edit: here is where sequencerGroups and sequencer are taken from:
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Sequencer.plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.sequencer = dict;
[dict release];
NSArray *array = [sequencer allKeys];
self.sequencerGroups = array;
Thanks a lot,
Maxim
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要从 rowNumber 创建索引路径,您可以使用:
[NSIndexPath indexPathForRow:<#(NSInteger)#> inSection:<#(NSInteger)#>];
这使得 row->indexPath 转换变得容易,一旦你有了 row &节号。
所以你的问题就变成了 - 我如何获得行& takeItemId 中的部分编号。
我认为我处理它的方法(但是,您可以考虑多种方法,具体取决于加载 plist 的执行时间的关键程度) - 在解析 plist 后,我将构造一个
NSDictionary< /code> 将
takeItemId
映射到 IndexPath。示例:
一旦有了
mapKeyToIndexPath
字典,当您在事件中收到takeItemId
时,就可以获取正确的 indexPath 并将其传递给[myTableDataSource tableView: myTableViewhighlightCellAtIndexPath:indexPath];
To create an indexPath from a rowNumber you can use:
[NSIndexPath indexPathForRow:<#(NSInteger)#> inSection:<#(NSInteger)#>];
This makes row->indexPath conversion easy, once you have the row & section numbers.
So your question then becomes - how do I get the row & section numbers from the takeItemId.
I think the way I would approach it (there are numerous ways you can consider however, depending on how critical the execution time of loading your plist is) would be - right after I parse the plist, I would construct an
NSDictionary
which maps thetakeItemId
to an IndexPath.An example:
Once you have the
mapKeyToIndexPath
dictionary, you can then grab the correct indexPath when you receive thetakeItemId
in your Event and pass it to[myTableDataSource tableView:myTableView highlightCellAtIndexPath:indexPath];
我会在 plist 中搜索 ID 以确定节和行是什么。
然后我将创建 NSIndexPath:
您还可以使用方法 rollToRowAtIndexPath:atScrollPosition:animated 滚动到单元格:
I would search the ID in the plist to determine what is the section and row.
Then I would create the NSIndexPath:
You can also scroll to the cell with the method scrollToRowAtIndexPath:atScrollPosition:animated: