2014-10-15 2 views
0

У меня есть текстовое поле, в котором пользователи вводят город, а также таблицу, чтобы заполнить города, которые пользователь выбирает. Однако, как только пользователь добавляет новый город, моя таблица не перезагружается, и по этой причине данные не обновляются в tableview. Я не знаю, чего мне не хватает. Я прикрепил свой код, а также скриншот.Перезагрузка TableView не работает

#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]; 
    [email protected]"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]; 
} 

enter image description here

ответ

2

Вы не добавляешь «» в то время как добавления другой строки и неправильную строки для создания массива, я думаю, что здесь проблема:

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

Надеется, что это помогает .. :)

Edit:

Лучшим способом было бы не использовать жало, просто используйте NSMutableArray. в viewDidLoad сделать это:

tableData = [[NSMutableArray alloc] init]; 
[tableDate addObject: @"Houston"]; 
[tableDate addObject: @"London"]; 
[tableDate addObject: @"Istanbul"]; 
[tableDate addObject: @"Tokyo"]; 

Тогда в вас addBtnClicked просто сделать это:

- (IBAction)addBtnClicked:(id)sender { 
    [tableDate addObject: addCityTF.text]; 
    [myTableView reloadData]; 
} 
Смежные вопросы