从 plist 字符串设置 UITableViewCell 标题



可能的重复项:
部分 UITableView 来源于 pList

我很难尝试将单元格的文本标签设置为我的 plist 中字符串的值。该应用程序在cell.textlabel.text = [array objectAtIndex:indexPath.row];崩溃。我很确定其他一切都很好,只是不知道最后一部分。提前谢谢。

这是我的清单:

Root - Array
    Item 0 - Dictionary
        Key 0 - Array
            Item 0 - String - Value 0
        Key 1 - Array
            Item 1 - String - Value 1

以下是我的表视图数据源方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return _objects.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSDictionary *dict = [_objects objectAtIndex:section];
return [dict.allValues count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
NSArray *array = [_objects objectAtIndex:indexPath.section];
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}

那么你_object的结构不清楚。我假设它指的是列表中的Item 0 - Dictionary

numberOfRowsInSection:中,您将其称为字典:

NSDictionary *dict = [_objects objectAtIndex:section]

而在cellForRowAtIndexPath:中,您将其称为数组:

NSArray *array = [_objects objectAtIndex:indexPath.section];

无论如何,如果我的假设是正确的,则将cellForRowAtIndexPath代码更改为以下内容:

NSDictionary *dict = [_objects objectAtIndex:indexPath.section];
NSarray *array = [dict objectForKey:myKey]; //myKey is whatever key you are assigning
cell.textLabel.text = [array objectAtIndex:indexPath.row];

希望这有帮助。

编辑:

好吧,你可以重组你的plist来实现这一目标。你基本上只是摆脱了dictionary级别。尝试这样的事情:

Root - Array
    Item 0 - Array                   <-- this is a section
        Item 0 - String - Value 0    <-- this is a cell
    Item 1 - Array                   <-- section
        Item 0 - String - Value 1    <-- cell

最新更新