Ios 如何使用NSDictionary中的数据更改UITableView中单元格的文本

Ios 如何使用NSDictionary中的数据更改UITableView中单元格的文本,ios,objective-c,uitableview,Ios,Objective C,Uitableview,当用户单击save按钮时,我将用户的信息存储在NSUserDefaults类型的变量中。 我想启动应用程序,设置可以保存在相应的单元格中填写。下面是我在viewDidLoad方法的相应文件中所做的 - (void)viewDidLoad { // [super viewDidLoad]; // Do any additional setup after loading the view. if (self.navigationPaneBarButtonItem)

当用户单击save按钮时,我将用户的信息存储在NSUserDefaults类型的变量中。 我想启动应用程序,设置可以保存在相应的单元格中填写。下面是我在viewDidLoad方法的相应文件中所做的

- (void)viewDidLoad
{
    // [super viewDidLoad];
// Do any additional setup after loading the view.

    if (self.navigationPaneBarButtonItem)
            [self.navigationBar.topItem setLeftBarButtonItem:self.navigationPaneBarButtonItem
                                                                                            animated:NO];

    //Initialise IndexPath
    NSIndexPath *indexPathBroker = [NSIndexPath indexPathForRow:0 inSection:0];
    NSIndexPath *indexPathPort = [NSIndexPath indexPathForRow:1 inSection:0];

    NSUserDefaults *defaultConf;
    defaultConf=[NSUserDefaults standardUserDefaults];
    NSString * broker =[defaultConf stringForKey:@"broker"];
    NSString *port= [defaultConf stringForKey:@"port"];
    NSLog(@"Brokerdidload: %@ et Portdidload: %@",broker,port);

    //Create strings and integer to store the text info
    ConfigDetailEditTableCell *editCellBroker = (ConfigDetailEditTableCell *)[_tableView cellForRowAtIndexPath:indexPathBroker];
    ConfigDetailEditTableCell *editCellPort = (ConfigDetailEditTableCell *)[_tableView cellForRowAtIndexPath:indexPathPort];


    //set data
          editCellPort.EditCellValue.text =port;
    editCellBroker.EditCellValue.text=broker;
  //  [editCellBroker.EditCellValue setText:broker];
  //   [[editCellPort textLabel] setText:port];
  //  [[editCellBroker textLabel] setText:broker];
   // [[editCellPort textLabel] setPlaceholder:port];
   // [[editCellBroker textLabel] setPlaceholder:broker];
   // editCellPort.EditCellValue.text =*/


    NSLog(@"editCellPort.EditCellValue: %@ et editCellBroker.EditCellValue: %@",editCellPort.EditCellValue.text,editCellBroker.EditCellValue.text);

}
代理和端口中的变量,我有信息,但当我尝试更新单元格时,它不起作用。我为空的最后一个NSLog引用

如何补救


提前感谢您

不要尝试从viewDidLoad设置单元格的值,您应该在tableView的cellForRowAtIndexPath:dataSource方法中进行设置

- (void)viewDidLoad
{
    // [super viewDidLoad];
// Do any additional setup after loading the view.

    if (self.navigationPaneBarButtonItem)
            [self.navigationBar.topItem setLeftBarButtonItem:self.navigationPaneBarButtonItem

   //Rest are not needed

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
       //Initialize cell


        NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
        NSString * broker =[defaults stringForKey:@"broker"];
        NSString *port= [defaults stringForKey:@"port"];

        cell.EditCellValue.text = indexPath.row==0?broker:port;

        return cell;

}

与其尝试从viewDidLoad设置单元格的值,不如在tableView的cellForRowAtIndexPath:dataSource方法中进行设置

- (void)viewDidLoad
{
    // [super viewDidLoad];
// Do any additional setup after loading the view.

    if (self.navigationPaneBarButtonItem)
            [self.navigationBar.topItem setLeftBarButtonItem:self.navigationPaneBarButtonItem

   //Rest are not needed

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
       //Initialize cell


        NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
        NSString * broker =[defaults stringForKey:@"broker"];
        NSString *port= [defaults stringForKey:@"port"];

        cell.EditCellValue.text = indexPath.row==0?broker:port;

        return cell;

}

您还可以使用免费的Sensive TableView框架自动从NSUserDefaults获取数据并将其显示在表视图上。它还将自动将输入的任何数据保存回NSUserDefaults。非常方便。

您还可以使用免费的Sensive TableView框架自动从NSUserDefaults获取数据并将其显示在表视图上。它还将自动将输入的任何数据保存回NSUserDefaults。非常方便。

为什么不在
cellforrowatinexpath
中测试索引路径并在那里设置值呢?此外,您不应该使用大写属性名称(如
EditCellValue
),否则其他人会非常不喜欢您的代码。此外,您应该始终调用
[super-viewDidLoad]当重写
viewDidLoad
时。应该是
viewdidappease
,否?我尝试过使用viewdidappease。我将viewDidLoad方法的内容复制并粘贴到ViewWillDisplay中。但是我也犯了同样的错误。为什么不在
cellforrowatinexpath
中测试indexpath并在那里设置值呢?此外,您不应该使用大写属性名称(如
EditCellValue
),否则其他人会非常不喜欢您的代码。此外,您应该始终调用
[super-viewDidLoad]当重写
viewDidLoad
时。应该是
viewdidappease
,否?我尝试过使用viewdidappease。我将viewDidLoad方法的内容复制并粘贴到ViewWillDisplay中。但我也有同样的错误。Pixx你可以考虑把答案标记为正确。快乐编码:)psix,你可以考虑把答案标记为正确。快乐编码:)