如何加载带有字典数组的tableView?
我想加载一个带有数组的表视图,该数组本身有多个字典。这些字典中的每一个都有我需要放入一行的项目,每行一个字典。所有这些字典都存储在一个数组中。
我应该如何执行这个任务?
谢谢,
请参阅下面的代码,项目是我的数组。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return self.projects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
cell.textLabel.text = [self.projects objectAtIndex:indexPath.row];
}
I want to load a table view with an array which itself has multiple dictionaries. Each of these dictionaries has items which I need to put into a row, one dictionary per row. All of these dictionaries are stored in an array.
How should I perform this task?
Thanks,
See the code below, projects is my array.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return self.projects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
cell.textLabel.text = [self.projects objectAtIndex:indexPath.row];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你几乎可以用你拥有的一切来做到这一点。您只需将
configureCell:atIndexPath:
方法更改为如下所示:只需将
some_key
更改为NSDictionary
中的键即可您想要在UITableViewCell
中显示。您还可以将
UITableViewCell
样式更改为UITableViewCellStyleSubtitle
,然后您可以将NSDictionary
中的另一个键添加到cell.detailTextLabel
>。You can do this with pretty much what you have. You'll just need to change your
configureCell:atIndexPath:
method to look something like this:Just change
some_key
to the key in yourNSDictionary
that you want to display in yourUITableViewCell
.You can also change your
UITableViewCell
style toUITableViewCellStyleSubtitle
and then you can add another key from yourNSDictionary
tocell.detailTextLabel
.除了 edc1591 之外,
如果数组中的字典有不同的键,那么
In addition to edc1591,
In case if the dictionaries in the array have different keys then