在NSMutableArray中的NSDictionary中显示tableView中的数据

时间:2011-07-25 09:13:52

标签: iphone objective-c arrays dictionary

我在UITableView中尝试存储记录,从表单重新加载数据。为此,当用户按下添加按钮,这样就应该将多个标签添加到第一个indexPath,然后再次重新点击添加按钮,必须先将多个单元格添加到第二个单元格中。

我希望将数据存储在

MyArray ({
  firstindex: 1st record
  firstindex: 2nd record
}
{
  secondindex : 1st record
  secondindex : 2nd record
})

记录来自UITextView,并在每个添加按钮上保存到表中,单击每行包含多个条目

- (IBAction) Add
{
      [myDict setObject:countryTxt.text forKey:@"Country"];
        [myDict setObject:cityTxt.text forKey:@"City"];
        [myDict setObject:EscortsNumTxt1.text forKey:@"people"];
        [myDict setObject:fromDate.text forKey:@"Datetogo"];
        [myDict setObject:toDate.text forKey:@"Datetoreach"];

        [moreArr addObject:myDict];

        NSLog(@"moreDict %@",moreArr);

        numberOfrows++;

        [moreTable reloadData];
}

我已经通过这种方式实现但是在TableView Cell上显示重复数据,我不包括UILabel代码的框架,因为只在fetchng中遇到问题

- (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]; 
}

countrylbl.text = [[moreArr objectAtIndex:indexPath.row] objectForKey:@"Country"];
        citylbl.text = [[moreArr objectAtIndex:indexPath.row] objectForKey:@"City"];
        people.text = [[moreArr objectAtIndex:indexPath.row] objectForKey:@"people"];
        toDateLbl.text = [[moreArr objectAtIndex:indexPath.row] objectForKey:@"Datetogo"];
        fromDteLbl.text = [[moreArr objectAtIndex:indexPath.row] objectForKey:@"Datetoreach"];
}

2 个答案:

答案 0 :(得分:0)

尝试像你的方法一样检索一次数组

- (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]; 
    }


    NSArray *results = [[NSArray alloc] initWithArray:[moreArr objectAtIndex:indexPath.row]];

    NSLog("%@",results); //Check that everything is in the right place

    // DO STUFF

    [results release];

}

答案 1 :(得分:0)

解决方案,因为NSMutableDictionary覆盖了我不知道的内容,所以我在函数中分配了它

    moreTableDict = [[NSMutableDictionary alloc] init];

    [myDict setObject:countryTxt.text forKey:@"Country"];
    [myDict setObject:cityTxt.text forKey:@"City"];
    [myDict setObject:EscortsNumTxt1.text forKey:@"people"];
    [myDict setObject:fromDate.text forKey:@"Datetogo"];
    [myDict setObject:toDate.text forKey:@"Datetoreach"];

    [moreArr addObject:myDict];

这对我有用