2013-03-04 69 views
1

我在我的程序中有文本框,按鈕和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; 
    } 
+0

是你用ARC嗎? – vikingosegundo 2013-03-04 08:27:06

+0

爲什麼你使用insertObject:方法而不是addObject:?因爲你只需要一個接一個地顯示對象。 – Anupdas 2013-03-04 08:39:40

回答

3

肯定你忘了創建數組

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

如果你沒有在你的代碼像這樣的任何線路,arraynil。許多新的Objective-C開發者都在爲事實而努力,即發送消息到nil是完全合法的。

+0

非常感謝你,現在有效。 – KenanYildirim 2013-03-04 08:32:57

+1

birşeydeğil:) – vikingosegundo 2013-03-04 08:36:03

+0

@vikingosegundo'birşeydeğil:)'這是什麼意思,它是哪種語言? – 2013-03-04 08:51:35

相關問題