Iphone 使用NSMutableArray填充UITableview

Iphone 使用NSMutableArray填充UITableview,iphone,ios,Iphone,Ios,试图用我的数组填充我的UItableview。我想将数组中的对象与正确的单元格相匹配 我的数组如下 ( ( { cost = 160; height = 1; room_number = 1; square_size = 1; title = "TIMBER BLINDS"; width = 1;

试图用我的数组填充我的
UItableview
。我想将数组中的对象与正确的单元格相匹配

我的数组如下

(
        (
                {
            cost = 160;
            height = 1;
            room_number = 1;
            square_size = 1;
            title = "TIMBER BLINDS";
            width = 1;
        }
    ),
        (
                {
            cost = 170;
            height = 1;
            room_number = 2;
            square_size = 1;
            title = "ROMAN BLINDS";
            width = 1;
        }
    )
我的问题是如何根据我的
数组中的房间号将正确的标题与单元格匹配


谢谢

假设您的数组是self.tableContent

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSDictionary *object = self.tableContent[index.section][index.row];

     ..... some cell code
     cell.title = object[@"title"];
}

它是数组的数组字典

您可以通过以下方式获取
“房间号”
的值

NSString *room_number = [[[myarray1 objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"room_number"]
()--这表示数组 {}--这表示字典

所以你可以看到你有一个数组,每个数组包含一个对象,也就是一个字典

就目标C而言,它是这样的:

NSDictionary *dict1 = //first dictionary
NSDictionary *dict2 = //second dictionary

NSArray *mainArray = [NSArray arrayWithObjects: [NSArray arrayWithObject:dict1],[NSArray arrayWithObject:dict2], nil];
要从字典中检索键标题内的值并在TableView中显示该值,请执行以下操作:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// other code to set up cell
NSDictionary *array =[[mainArray objectAtIndex:indexPath.row] objectAtIndex:0];
cell.textLabel.text = [array objectForKey:@"title"];
return cell;
}

看起来您有一个包含字典的数组数组,所以首先通过indexPath.row从数组中获取对象。获取的对象也是一个数组。通过objectAtIndex从中获取字典对象。既然您已经访问了字典,那么就可以访问键值了

NSMutableArray *fetchedArray= [yourArray objectAtIndex:indexPath.row];
NSDictionary *fetchedDictionary = [fetchedArray objectAtIndex:0];

NSNumber *roomNumber= [fetchedDictionary valueForKey:@"room_number"];
if([roomNumber intValue] == indexPath.row) {
    [cell setTextLabel:[fetchedDictionary valueForKey:@"title"]];
}

首先为您的对象实现一个类!谢谢Ranju,这对第一个细胞有效,但对第二个细胞无效。使用[\uu NSArrayM objectAtIndex:]:索引1超出边界[0..0]将尝试此操作!谢谢你,你是一个传奇!!工作得很有魅力!