【问题标题】:how to show the sqlite data in uitableview on otherviewcontroller?如何在otherviewcontroller的uitableview中显示sqlite数据?
【发布时间】:2013-12-11 05:10:06
【问题描述】:

我有 viewcontroller .h .m 和 .xib,其中包含用于插入更新和删除的注册表单。

我还有其他包含显示 sqlite 数据的 uitableview 的 xib。我如何显示???

我编写了代码,但它在 viewcontroller 页面数组和 viewcontroller 中使用的其他控件上给出了错误。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                      reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [[data objectAtIndex:indexPath.row] valueForKey:@"name"];
    cell.detailTextLabel.text = [[data objectAtIndex:indexPath.row] valueForKey:@"salary"];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    name.text = [[data objectAtIndex:indexPath.row] valueForKey:@"name"];
    salary.text = [[data objectAtIndex:indexPath.row] valueForKey:@"salary"];
}

这里的数据是NSArray,在viewcontroller.h 文件中会出现错误

而且在didSelectRowAtIndexPath 方法name.textsalary.text 中也会出错,因为它是viewcontroller 的文本字段。

我该如何解决?????

【问题讨论】:

    标签: ios iphone objective-c sqlite uitableview


    【解决方案1】:

    您必须确保数据数组应该在 otherViewController 中可用。为此,在 OtherViewController 的 .h 文件中创建一个数组属性,并在加载此视图控制器时,将数据数组分配给此属性。

    当您使用 SQlite 数据库时,您可以在 OtherViewController 中从数据库中获取数据(您需要添加额外的代码)并将其显示在 tableView 那里。

    您也不能在不同的视图控制器中访问视图控制器的私有变量。您必须为它们创建属性或创建公共方法。

    【讨论】:

    • 你好thanx回复我...我想问我在viewcontroller中以编程方式创建uitable。并且otherviewController有注册表。当我点击otherviewcontroller的提交按钮时,它会在viewcontroller的tableview中显示sqlite数据.....可以这样做..因为Otherviewcontroller和tableview上的注册表在Viewcontroller.. .请它非常小......
    • 在拥有tableView的viewController的-viewWillAppear中,重新加载数据库中的数据,重新加载tableView。要重新加载 tableView,请使用 [tableView reloadData]。只有在刷新了 tableview 的数据源后才调用它。
    【解决方案2】:

    这样做..首先创建临时字典,然后从中获取数据..

    NSDictionary * tmpDictn = [data objectAtIndex:indexPath.row];
    cell.textLabel.text = [tmpDictn valueForKey:@"name"];
    cell.detailTextLabel.text = [tmpDictn valueForKey:@"salary"];
    

    这里也一样..

    NSDictionary * tmpDictn = [data objectAtIndex:indexPath.row];
    name.text = [tmpDictn valueForKey:@"name"];
    salary.text = [tmpDictn valueForKey:@"salary"];
    

    【讨论】:

      猜你喜欢
      • 2012-08-22
      • 2013-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多