Reputation: 16793
I have a textfield that users enter the city and also tableview to populate cities that user chooses. However once user add a new city, my table does not reload and for that reason data does not update on tableview. I do not know what I am missing. I have attached my code as well as screenshot.
#import "AddCityViewController.h"
#import "ViewController.h"
@interface AddCityViewController ()
@end
@implementation AddCityViewController
@synthesize addCityTF;
@synthesize myTableView;
@synthesize tableData;
@synthesize myString;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
tableData = [[NSMutableArray alloc] initWithCapacity:0];
myString=@"Houston,London,Istanbul,Tokyo";
NSArray *array = [myString componentsSeparatedByString:@","];
tableData = [NSMutableArray arrayWithArray:array];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
NSLog(@"Data is :%@",[tableData objectAtIndex:indexPath.row]);
cell.textLabel.font = [UIFont fontWithName:@"BebasNeue" size:24];
return cell;
}
- (IBAction)addBtnClicked:(id)sender {
NSString* newString=[myString stringByAppendingString:addCityTF.text];
NSArray *array = [myString componentsSeparatedByString:@","];
tableData = [NSMutableArray arrayWithArray:array];
[myTableView reloadData];
//[self performSegueWithIdentifier:@"Main2Detail" sender:self];
}
Upvotes: 0
Views: 45
Reputation: 11197
You are not adding ',' while appending another string and wrong string for making the array, I think here is the problem:
- (IBAction)addBtnClicked:(id)sender {
NSString* tempString=[myString stringByAppendingString:@","];
NSString* newString=[tempString stringByAppendingString:addCityTF.text];
NSArray *array = [newString componentsSeparatedByString:@","];
tableData = [NSMutableArray arrayWithArray:array];
[myTableView reloadData];
//[self performSegueWithIdentifier:@"Main2Detail" sender:self];
}
Hope this helps.. :)
Edit:
Better way would be don't use sting, just use a NSMutableArray
. in viewDidLoad do this:
tableData = [[NSMutableArray alloc] init];
[tableDate addObject: @"Houston"];
[tableDate addObject: @"London"];
[tableDate addObject: @"Istanbul"];
[tableDate addObject: @"Tokyo"];
Then In you a addBtnClicked
just do this:
- (IBAction)addBtnClicked:(id)sender {
[tableDate addObject: addCityTF.text];
[myTableView reloadData];
}
Upvotes: 2