2016-11-08 51 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