Ios 在tableview中显示数组和字典值

Ios 在tableview中显示数组和字典值,ios,objective-c,nsmutablearray,Ios,Objective C,Nsmutablearray,我创建了一个数组、两个字典和一个tableview。tableview应该显示数组的内容,该数组包含字典的数据。当我尝试使用“[mArray addObject:[mDictionary]”显示它时,会引发异常,但如果我使用“[mArray addObject:[mDictionary objectForKey:@“firstname]”]”;“工作正常 您能帮助我理解为什么会发生这种情况,并且不使用“objectForKey”就不能显示字典值吗 这是我的“ViewController.m” 如

我创建了一个数组、两个字典和一个tableview。tableview应该显示数组的内容,该数组包含字典的数据。当我尝试使用“[mArray addObject:[mDictionary]”显示它时,会引发异常,但如果我使用“[mArray addObject:[mDictionary objectForKey:@“firstname]”]”;“工作正常

您能帮助我理解为什么会发生这种情况,并且不使用“objectForKey”就不能显示字典值吗

这是我的“ViewController.m”


如果尝试将some文本指定给单元格的textlabel,则返回的对象来自:

[mArray objectAtIndex:indexPath.row];
应该是一个字符串,而不是整个字典

当您将整个字典放入数组
[mArray objectAtIndex:indexPath.row]
将返回一个字典,您无法将字典分配给textlabel,因此会出现异常。如果要存储整个词典,请使用
objectForKey
获取所需字符串:

cell.textLabel.text = [[mArray objectAtIndex:indexPath.row] objectForKey:@"WHAT_EVER_KEY_YOU_WANT"];

顺便说一句,您没有使用zo,您可能希望获得一个自动删除的UITableViewCell,因此将
自动删除
添加到您的单元格中:

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] autorelease];


数组包含类似类型的对象。可以将字典对象添加到数组中。在那种情况下没有问题。问题是当您从数组中获取一个元素并分配给cell.textLabel.text时。这里的对象是dictionary的类型,cell.textLabel.text是nsstring的类型。所以,您应该在cell.textlab.text中为dictionary对象的键指定一个值

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = [[mArray objectAtIndex:indexPath.row] objectForKey:@"firstname"];
cell.detialTextLabel.text = [[mArray objectAtIndex:indexPath.row] objectForKey:@"lastname"];
        return cell;
    }

我认为这将对您有所帮助。

在.h文件中声明一个数组:

NSArray *keys
在viewDidLoad中编写此代码

keys = [mDictionary allKeys];
这是cellForRowAtIndexPath中的

cell.textLabel.text = [mDictionary objectForKey:[keys objectAtIndex:indexPath.row];

这解决了我的问题

NSDictionary *cellDict = [self.tableInfoArr objectAtIndex:indexPath.row];

//This is to don't care about what is key and value in that dictionary
NSArray *keyArr = [cellDict allKeys];

//As at each index of array only one dictionary exist.
NSString *keyStr = [keyArr objectAtIndex:0];

cell.titleLabel.text =  keyStr;
cell.detailLabel.text =  [cellDict valueForKey:keyStr];

我使用了上面的代码,但它只显示firstname值。将UITableViewCellStyleDefault更改为UITableViewCellStyleSubtitle,还应更改所有字典对象中的键名称。这意味着如果不使用键值,我们无法显示字典数据?是,并使用
objectForKey
获取正确的NSString对象。好。。。如果需要,也可以使用字典对textlabel的描述:
[[mArray objectAtIndex:indexPath.row]description]您在这方面面临什么问题?
cell.textLabel.text = [mDictionary objectForKey:[keys objectAtIndex:indexPath.row];
NSDictionary *cellDict = [self.tableInfoArr objectAtIndex:indexPath.row];

//This is to don't care about what is key and value in that dictionary
NSArray *keyArr = [cellDict allKeys];

//As at each index of array only one dictionary exist.
NSString *keyStr = [keyArr objectAtIndex:0];

cell.titleLabel.text =  keyStr;
cell.detailLabel.text =  [cellDict valueForKey:keyStr];