【问题标题】:TableView isn't updating data even though it recognizes input?TableView 即使识别输入也不更新数据?
【发布时间】:2013-02-18 12:12:25
【问题描述】:

所以我有 5 行,并且在选择时它们将带有行号的整数传递给我的第二个视图控制器。

每个数字都有自己的包含项目的数组,然后应该返回指定行的项目数量。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MasterCell";

    MasterCell *cell = (MasterCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];


if (radInt == 0) {
     cell.textLabel.text = @"LM";
     NSLog(@"My return should have been LM");

}
if (radInt == 1) {
    cell.textLabel.text = @"Restauranger";
    NSLog(@"My return should have been Rest");
}
    if (radInt == 2) {
        cell.textLabel.text = [shoppingArray objectAtIndex:indexPath.row];
    }
    if (radInt == 3) {
        cell.textLabel.text = [annatArray objectAtIndex:indexPath.row];
    }

    //cell.textLabel.text = [listData objectAtIndex:[indexPath row]];
    cell.imageView.image = [UIImage imageNamed:@"tab-icon1.png"];
    cell.accessoryType = UITableViewCellAccessoryNone;


    return cell;
}

这是我的代码,只是为了测试它,它不起作用。 NSLOG 工作正常,但数据根本不会更新......我做错了什么?它每次都 Nslogs LM,但它在日志中也有 1 是选定的行 (radInt)。

新方法

static NSString *CellIdentifier = @"MasterCell";
    MasterCell *cell = (MasterCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell==nil)
        cell = [[MasterCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    if (radInt == 0) {
        cell.textLabel.text = @"LM";
        NSLog(@"My return should have been LM");

    }
    else if (radInt == 1) {
        cell.textLabel.text = @"Restauranger";
        NSLog(@"My return should have been Rest");
    }
    else if (radInt == 2) {
        cell.textLabel.text = [shoppingArray objectAtIndex:indexPath.row];
    }
    else if (radInt == 3) {
        cell.textLabel.text = [annatArray objectAtIndex:indexPath.row];
    }

    else {
        cell.textLabel.text = @"Noes";
    }


    //cell.textLabel.text = [listData objectAtIndex:[indexPath row]];
    cell.imageView.image = [UIImage imageNamed:@"tab-icon1.png"];
    cell.accessoryType = UITableViewCellAccessoryNone;

    return cell;

----之前的景色

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SecondViewController *enjoy = [[SecondViewController alloc] init];
    if ([[array objectAtIndex:indexPath.row] isEqual:@"Livsmedel"]) {
        [enjoy setTitle:[array objectAtIndex:indexPath.row]];
        enjoy.radInt = 0;
        NSLog(@"0");
    }

    if ([[array objectAtIndex:indexPath.row] isEqual:@"Restauranger"]) {
        [enjoy setTitle:[array objectAtIndex:indexPath.row]];
        enjoy.radInt = 1;
        NSLog(@"1");
    }

    if ([[array objectAtIndex:indexPath.row] isEqual:@"Shopping"]) {
        [enjoy setTitle:[array objectAtIndex:indexPath.row]];
        enjoy.radInt = 2;

    }
    if ([[array objectAtIndex:indexPath.row] isEqual:@"Annat"]) {
        enjoy.radInt = 3;
    }

    [self performSegueWithIdentifier:@"main" sender:self];
}

【问题讨论】:

  • indexPath 和您的选择变量 (radInt) 之间似乎没有任何联系。
  • 你登录 radInt 看看它是什么?你是如何将它传递到这个表格视图中的?
  • 在之前的视图中我有上面的方法,现在添加
  • 我在第二个视图中记录了变量 radInt,它确实有效,所以我想问题出在表数据中。
  • 问题是,您正在使用 alloc init 创建 SecondViewController 的新实例,而不是获取情节提要中的实例。

标签: iphone ios objective-c xcode uitableview


【解决方案1】:

您实际上不需要实现 didSelectRowAtIndexPath: 如果您将情节提要中的 segue 从单元连接到下一个控制器。你需要实现的是 prepareForSegue:。您的代码应如下所示:

- (void)prepareForSegue:(UIStoryboardSegue *) segue sender:(id) sender
{
    NSInteger row = [self.tableView indexPathForSelectedRow].row;
    SecondViewController *enjoy = segue.destinationViewController;
    if ([[array objectAtIndex:row] isEqual:@"Livsmedel"]) {
        [enjoy setTitle:[array objectAtIndex:row]];
        enjoy.radInt = 0;
        NSLog(@"0");
    }

    if ([[array objectAtIndex:row] isEqual:@"Restauranger"]) {
        [enjoy setTitle:[array objectAtIndex:row]];
        enjoy.radInt = 1;
        NSLog(@"1");
    }

    if ([[array objectAtIndex:row] isEqual:@"Shopping"]) {
        [enjoy setTitle:[array objectAtIndex:row]];
        enjoy.radInt = 2;

    }
    if ([[array objectAtIndex:row] isEqual:@"Annat"]) {
        enjoy.radInt = 3;
    }
}

【讨论】:

  • 非常感谢:D 确实有效。我不知道您可以做 prepareForSegue 以在情节提要中发送数据。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多