我在我的程序中有文本框,按鈕和UItableview
。用戶在文本框中輸入文本,當他點擊按鈕時,我想在tableviewcell
上顯示他的文本。他會再次輸入一個文本,並且我會在單元格0中顯示他的舊文本,並且新文本在單元格1中顯示。一次又一次... 這個問題看起來很簡單。當用戶點擊發送按鈕時,我將一個對象添加到NSMutableArray
,但數組數總是返回0.任何人都可以幫助我,thx。如何以編程方式將其添加到NSMutableArray並在tableviewcells上顯示它?
這裏是我的代碼:
int s=0;
-(IBAction)btn_Click:(id)sender{
[array insertObject:txt.text atIndex:s];
s++;
[tableView reloadData];
}
//mytableview delegates
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [array count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[[UITableViewCell alloc]init];
cell.textLabel.text=[array objectAtIndex:indexPath.row];
return cell;
}
是你用ARC嗎? – vikingosegundo 2013-03-04 08:27:06
爲什麼你使用insertObject:方法而不是addObject:?因爲你只需要一個接一個地顯示對象。 – Anupdas 2013-03-04 08:39:40