gpt4 book ai didi

ios - NSDictionary allkeys 到 UITableViewCell

转载 作者:行者123 更新时间:2023-11-28 19:03:57 24 4
gpt4 key购买 nike

我正在尝试将我的 NSdictionary 值放入 UITableViewCell 中。这是我的字典格式:

 {
date = "3/4/14, 3:33:01 PM Pacific Standard Time";
weight = 244;
}

这是我用来填充 uitableview 的代码(无法正常工作)。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"WeightCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}


NSArray* allKeys = [weights allKeys];

NSDictionary *obj = [allKeys objectAtIndex: indexPath.row];

cell.textLabel.text = [obj objectForKey: @"date"];
cell.detailTextLabel.text = [obj objectForKey:@"weight"];

return cell;
}

最佳答案

您应该尝试在 tableView 本身之外为 tableView 初始化数组...类似于

- (void)viewDidLoad
{
[super viewDidLoad];

_allKeys = [[NSMutableArray alloc] initWithArray:[weights allKeys]];

}

一旦您初始化了该数据,您就可以在整个过程中访问它。还要找出您的 tableview 需要多少行。

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_allKeys count];
}

然后,当您将数组访问到字典中时,它保留了行数并且可以正确访问它。

NSDictionary *obj = [_allKeys objectAtIndex: indexPath.row];

cell.textLabel.text = [obj objectForKey: @"date"];
cell.detailTextLabel.text = [obj objectForKey:@"weight"];

据我所知,字典无法访问 indexPath.row 中的数组,因为在 tableView 中使用它之前,您还没有在任何地方初始化数组。

希望对你有帮助,T

关于ios - NSDictionary allkeys 到 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22185695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com