2013-06-12 65 views
0

我開始與iOS有一個愚蠢的問題。我只是想顯示一個TableView填充存儲在NSMutableArray中的字符串。我可以看到字符串在數組中,但由於某種原因,TableView沒有顯示它們。Tableview控制器不顯示來自NSMutableArray的字符串來源

我有bascially這樣的:

@interface Test() 
@property (weak, nonatomic) IBOutlet UITableView *contactList; 
@property (strong, nonatomic) NSMutableArray *contactsArray; 
@end 

- (void)onContactFound:(NSString*)contact 
{ 
    [self.contactsArray addObject:contact]; 
} 

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

//4 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    //5 
    static NSString *cellIdentifier = @"SettingsCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    //6 
    NSString *tweet = [self.contactsArray objectAtIndex:indexPath.row]; 
    //7 
    [cell.textLabel setText:tweet]; 
    [cell.detailTextLabel setText:@"via Codigator"]; 
    return cell; 
} 

我認爲這個問題是在最後一部分。我從一個例子中複製了這段代碼(http://www.codigator.com/tutorials/ios-uitableview-tutorial-for-beginners-part-1/),說我應該添加一些動態屬性,但在我的TableView中,我沒有屬性檢查器中的這些屬性,所以基本上我沒有@「SettingsCell」,所以我猜這是至少問題,也許這個代碼不適用於我的情況,它應該以另一種方式完成?

回答

2

我認爲你正試圖在沒有創建單元格的情況下出隊。我想你只能得到零個單元格。你應該使用這樣的事情:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease]; 
} 

也有其規定的API文檔中看看:

dequeueReusableCellWithIdentifier:返回位於其標識的可重複使用的表視圖單元對象。 返回值:具有關聯標識符的UITableViewCell對象,或者如果在可重用單元隊列中不存在此類對象,則爲零。

dequeueReusableCellWithIdentifier:

+0

所以恐怕您的教程是錯在這方面,它指出:「因此,我們的方法在這裏告訴操作系統給我們提供的小區ID的可重複使用的電池如果沒有找到,創建一個新的一。」該方法不會創建一個新的(如他的代碼示例所示),而是您必須手動創建它。 – naeger

+0

非常感謝您的幫助。那是缺失的部分。 – user1816142

+0

如果您已經在故事板中將單元標識符設置爲SettingsCell,則不需要檢查單元是否爲零並現在分配init。在教程中給出瞭如何設置,我會建議你這樣使用它。如果您在同一個表格中使用多種類型的單元格,這將非常有用。對於任何其他查詢,您可以使用教程中的註釋部分,我會很樂意回答他們。 –