如何通过单元格的文本数据获取 NSIndexPath(对于 UITableCell)?

发布于 2025-01-08 16:20:25 字数 2911 浏览 0 评论 0原文

在我的应用程序中,我有一个表视图控制器,它使用 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵你手 2025-01-15 16:20:25

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

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

所以你的问题就变成了 - 我如何获得行& takeItemId 中的部分编号。

我认为我处理它的方法(但是,您可以考虑多种方法,具体取决于加载 plist 的执行时间的关键程度) - 在解析 plist 后,我​​将构造一个 NSDictionary< /code> 将 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 字典,当您在事件中收到 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 the takeItemId to an IndexPath.

An example:

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

Once you have the mapKeyToIndexPath dictionary, you can then grab the correct indexPath when you receive the takeItemId in your Event and pass it to [myTableDataSource tableView:myTableView highlightCellAtIndexPath:indexPath];

负佳期 2025-01-15 16:20:25

我会在 plist 中搜索 ID 以确定节和行是什么。
然后我将创建 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];

您还可以使用方法 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:

//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];

You can also scroll to the cell with the method scrollToRowAtIndexPath:atScrollPosition:animated:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文