【问题标题】:Null data when loading DetailView加载DetailView时为空数据
【发布时间】:2013-10-23 08:11:59
【问题描述】:

我有 UITableView 我想单击行并加载详细视图,但我的登录详细视图中有空信息 你能帮我赢得这个实施吗

提前致谢!

cellForRowAtIndexPath 方法:

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

//Load Cell for reuse
static NSString *CellIdentifier = @"TestCell";
TestCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell =[ [[NSBundle mainBundle] loadNibNamed:@"TestCell" owner:nil options:nil] 
lastObject];
}

_tests = [server info];
_t = [NSMutableString stringWithFormat:@"%@ ", [_tests objectAtIndex:indexPath.row]];

NSString *testdata = _t;

_components = [testdata componentsSeparatedByString:@","];

for (NSString *aComponent in _components) {
    if ([aComponent hasPrefix:@"titletest"]) {
        _subComponents = [aComponent componentsSeparatedByString:@":"];
        _testString = [_subComponents[1] stringByTrimmingCharactersInSet:[NSCharacterSet
 characterSetWithCharactersInString:@"\""]];

    }if ([aComponent hasPrefix:@"note"]){
        _subComponents = [aComponent componentsSeparatedByString:@":"];
        _noteString = [_subComponents[1] stringByTrimmingCharactersInSet:[NSCharacterSet
characterSetWithCharactersInString:@"\""]];

        break;
    }
}




cell.title.text = _testString;
cell.note.text = _noteString;

return cell;

}

didSelectRowAtIndexPath 方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath 
*)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];


TestDetailView *c = [[TestDetailView alloc] init];

///I don't know what should I put here to c 

[self.navigationController pushViewController:c animated:YES];

}

但在我的详细视图中

- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:YES];


 NSLog(@" test : %@",_Info.datas);   ==>> my log was (null)

}

我的详细视图标题

@property (strong, nonatomic) IBOutlet UILabel *bDetailTitle;
@property (strong, nonatomic) IBOutlet UILabel *bDetailNote;
@property (nonatomic, strong) NSArray *datas;
@property (nonatomic,strong) TestTable *Info;

@结束

【问题讨论】:

  • 看起来您正在将 TestDetailView c* 设置为在分配后直接指向您的数据对象之一。这很奇怪,因为这可能会引发警告。也许你想要这样的东西:c.datas = [_datas objectAtIndex:indexPath.row]; 没有更多的代码,很难说。
  • 另外,在didSelectRowAtIndexPath 中你能告诉我们_datas 包含什么吗?我猜它包含您的数据模型对象,而不是 UIViewControllers,但我可能是错的。
  • @Aaron in _datas 是我从服务器获取的信息
  • 你的意思是从 _datas 设置一个对象到 TestDetailView 上?
  • @Aaron 我设置 c.datas 时出错,但我有一个错误 datas is not object of TestDetailView

标签: objective-c uitableview


【解决方案1】:

我假设TestDetailView 是一个ViewController 子类,并且您在它上面有一个可能称为datas 的属性。如果是这种情况,您似乎在 didSelectRowAtIndexPath: 中创建了您的详细视图控制器,如下所示:

TestDetailView *c = [[TestDetailView alloc] init];

但是然后立即将它的指针更改为其他东西(可能是nil):

c = [_datas objectAtIndex:indexPath.row];

objectAtIndex 返回一个 id 这意味着您的代码可能不会在 Xcode 中引发警告,但在运行时如果 [_datas objectAtIndex:indexPath.row] 没有返回 UIViewController(我怀疑它没有)你的应用程序可能会崩溃。如果它没有抛出错误或崩溃,您可能还有另一个问题,即 _datas 在您的父视图控制器中为零。

将值传递给视图控制器是一种非常常见的技术,我建议您阅读如何执行此操作。此类问题已在 S.O. 上提出。多次:

How to set value for NSString in another view controller from one viewcontroller

【讨论】:

  • 让我知道你想看什么我更新了你可以看到标题的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-02
  • 2011-05-06
  • 1970-01-01
  • 2021-02-23
  • 2020-09-22
  • 1970-01-01
  • 2015-01-18
相关资源
最近更新 更多