2012-11-21 31 views
0

我已經創建了一個表格視圖控制器,其上有UITextField。如何填充來自UITextField的輸入文本的tableview控制器單元。我嘗試以下,但它似乎數組爲空或被清空如何使用文本輸入填充表格視圖控制器

- (IBAction)endingInput:(id)sender{ 
    [sender resignFirstResponder]; 

    //send the message to the table cell 
    NSMutableArray *history = [[NSMutableArray alloc] init]; 
    [history addObject:[NSString stringWithFormat:@"%@",self.chatMessageField.text]]; 
    [self.chatHistory addObject:history]; 
    [myTableView reloadData]; 

    //push the message to server 
    [self initiateMessageSending:self.chatMessageField.text]; 

    chatMessageField.text = @""; 
    [history release]; 
} 

在我cellForRowAtIndex方法;

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

// Configure the cell... 
if(cell == nil){ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

if(tableView == self.myTableView){ 
    cell.textLabel.text = [chatHistory objectAtIndex:indexPath.row]; 
} 
return cell; 

我已經設置了一箇中斷點,但它並沒有通過那裏。我懷疑我的陣列是空的。

同樣,哪裏是最好的地方叫reloadData方法?

另外,chatHistory是私人會員,有沒有一種方法來初始化它像[[self.chatHistory alloc] init];

我知道它的一個常見問題,但我一直在討論這個問題一段時間了。

+0

你初始化chatHistory? –

+0

@PratyushaTerli - 現在我做了,但仍然無濟於事,我有NSLog我的數組,它表明它有編碼的文本輸入我做了什麼,我不能做的是讓數組的內容顯示到tableview cell –

+0

註釋掉'if(tableView == self.myTableView){'和'}'並且試試 –

回答

0

我想你忘記了alloc和初始化您chatHistory

- (void)viewDidLoad 
{ 
    self.chatHistory = [[NSMutableArray alloc] init]; 
} 
+0

感謝您的提醒! –

+0

不客氣!如果你已經解決了你的問題他/她的答案,你應該接受一些人的答案。 – sunkehappy

0

如果你沒有初始化chatHistory它甚至不存在了,從來沒有介意是空的! (我不清楚「私人會員」在這裏是什麼意思。)

如果您記錄的值爲chatHistory,那麼您將確定它是否爲零或爲空。

我看到的另一個問題是,您正試圖將chatHistory與其他(history)數組一起加載,但將其內容用作字符串。你應該決定它是一個數組還是一串字符串,然後保持一致。

(也[[self.chatHistory alloc] init];不是創建新對象有效的語法。看一個Objective-C language reference的正確方法。)

+0

謝謝指出! –

相關問題