KenanYildirim
KenanYildirim

Reputation: 41

How can I add programatically to NSMutableArray and show it on tableviewcells?

I have textfield,button and UItableview in my program.The user enters a text in textfield and when he clicks the button ı want to show his text on tableviewcell. He will enter again a text and ı will show him old text in cell-0 and new text is on cell-1. Again and again... the problem looks like simple.I add an object to NSMutableArray when user clicks the send button,but array count always returns 0.Can anyone help me,thx.

Here is my code:

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;
   }

Upvotes: 1

Views: 834

Answers (1)

vikingosegundo
vikingosegundo

Reputation: 52227

for sure you forgot to create the array

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

If you dont have any line like this in your code, array is nil. Many new Objective-C developer struggle with the fact, that it is totally legal to send messages to nil.

Upvotes: 3

Related Questions