2016-11-08 157 views
0

我的tableview中的单元格不会显示任何内容。Objective c UITableViewCell不显示内容

- (void)viewDidLoad { 
[super viewDidLoad]; 
testArray = [[NSMutableArray alloc] init];  
} 

如果按钮被点击,应用程序将转到下一个屏幕并显示表格。 (日志显示testString被存储到testArray中)

- (IBAction)goNext:(id)sender { 

    for(int i=0; i<10; i++){   
      NSString *testString = @"Hello"; 
      [testArray addObject:testString]; 
      NSLog(@"myArray:\n%@", testArray[i]);   
     } 
     UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
     UIViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"test"]; 
     [self presentViewController:vc animated:YES completion:nil]; 

} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return testArray.count; 
} 

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

static NSString *cellId = @"cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId]; 

cell.textLabel.text = testArray[indexPath.row]; 
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", (int)indexPath.row + 1]; 

return cell; 

} 

如果我这样做,将显示内容。

- (void)viewDidLoad { 
[super viewDidLoad]; 
[self testArraySetup];  
} 

- (void)testArraySetup{ 

testArray = [NSMutableArray arrayWithArray:@[@"Pizza",@"Bread",@"Drinks"]]; 

} 

任何意见是非常感谢。

+0

你在哪里调用'reloadData'? – rmaddy

+0

'[tableView reloadData];'或者检查'delegate'和'dataSource' – Rin

+0

问题已更新,请再次检查。 – bubibu

回答

2

的答案如果你想一个控制器的测试数组传递给另一个UIViewController的B关于点击下一步按钮,你应该让testArray的财产的UIViewController B中你想要显示你的tableView。

// UIVIewController A 
@interface ViewControllerA() 
{ 
    NSMutableArray * testArray; 
} 
    - (void)viewDidLoad { 
    [super viewDidLoad]; 
    testArray = [[NSMutableArray alloc] init];  
    } 

     - (IBAction)goNext:(id)sender { 

      for(int i=0; i<10; i++){   
        NSString *testString = @"Hello"; 
        [testArray addObject:testString]; 
        NSLog(@"myArray:\n%@", testArray[i]);   
       } 
       UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
       ViewControllerB *vc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"test"]; 
       vc.testArray = testArray; 
       [self presentViewController:vc animated:YES completion:nil]; 

     } 

    // ViewController B 
    @interface ViewControllerB : UIViewController 
     @property (nonatomic,retain)NSMutableArray * testArray; 

无需重新初始化testArray中的UIViewController viewDidLoad中B.Otherwise它会重新分配内存testArray。

+0

有一个错误:在'UIViewContoller'类型的对象上找不到属性testArray – bubibu

+0

创建此属性到UIViewController类的接口你实现了tableView委托。 – Daggarwal

+0

vc对象应该是ViewControllerB类的一个实例,而不是像这样的UIViewController“ViewControllerB * vc = [mainStoryBoard instantiateViewControllerWithIdentifier:@”test“];” – Daggarwal

1

这条线......

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId]; 

...不创建一个单元格,如果没有已经在重用队列细胞(这是初始状态时,应用程序第一次运行时会返回零而且你还没有明确地创建任何单元格)。这意味着你的整个方法将返回零(即没有单元格)。

使用时,您还需要检查cell == nil如果是这样,那么你需要明确自己创建一个新的单元格的东西,如:

if (! cell) 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; 

或者,您可以为标识注册类然后使用[tableView dequeueReusableCellWithIdentifier: forIndexPath:]代替,它将根据您注册的类生成一个单元格。

关于两者之间的差异信息,请参阅:When to use dequeueReusableCellWithIdentifier vs dequeueReusableCellWithIdentifier : forIndexPath

+0

谢谢你,但你的解决方案不起作用。 – bubibu

+0

请发布您的更新代码。我的解决方案可能只是问题的一部分(如其他答案中所述)。如果你不创建任何单元格,它肯定无法工作! –

1

可能出现的问题的解决方案:

1:您没有为UITableView设置datasourcedelegateself正常。

解决方案:在添加这些行你ViewDidLoadViewDidAppear

yourTableView.datasource = self; 
yourTableView.delegate = self; 

2:你有没有让故事板为您TableView适当IBOutlet连接。

解决方案:创建@property (nonatomic, weak) IBOutlet UITableView *tableView;现在转到故事板,在连接检查器下,您可以通过控制拖动它将您的属性与故事板中的tableView连接起来。

3:从你的代码,就好像你正在使用datasourcedelegate方法TableViewViewControllerA和你想显示ViewControllerB内容。

解决方案:您应该在各自的ViewControllerB类中实现UITableView的datasourcedelegate

4:您没有正确初始化/重新使用UITableViewCell

解决办法:看@儿子A的海滩

+0

我尝试了所有可能的解决方案,但不是他们的作品;(欣赏它无论如何。 – bubibu