I have four textfields like textfield1, textfield2, textfield3, textfield4 and one button. When I click on the button I am adding the textfield text to an NSMutableArray and populating in a tableview.
My code is:
- (void)buttonClick
{
NSMutableArray *array =[[NSMutableArray alloc]init];
[array addObject: textfield1.text];
[array addObject: textfield2.text];
[array addObject: textfield3.text];
[array addObject: textfield4.text];
}
Populating in table view using delegate methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text =[array objectAtIndex:indexPath.row];
return cell;
}
The above code is working fine. But when I click on button without entering any data in the textfields, the app is crashing because the array is empty. How to solve this problem?