1

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;
   }
1
  • Why are you using insertObject: method instead of addObject:? As you just need to show objects one after another. Commented Mar 4, 2013 at 8:39

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.