1

当用户单击保存按钮时,我将用户的信息存储在 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);

}

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

怎么补救????

先感谢您

4

2 回答 2

1

与其尝试从 viewDidLoad 设置单元格的值,不如在 cellForRowAtIndexPath: tableView 的 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;

}
于 2013-03-25T10:49:12.283 回答
0

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

于 2013-03-25T19:04:26.990 回答