简体   繁体   English

重新加载TableView不起作用

[英]Reloading TableView not working

I have a textfield that users enter the city and also tableview to populate cities that user chooses. 我有一个文本字段,用户可以输入城市,还可以使用tableview填充用户选择的城市。 However once user add a new city, my table does not reload and for that reason data does not update on tableview. 但是,一旦用户添加了新城市,我的表就不会重新加载,因此,数据不会在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];
}

在此处输入图片说明

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 . 更好的方法是不使用字符串,仅使用NSMutableArray in viewDidLoad do this: 在viewDidLoad中执行以下操作:

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

Then In you a addBtnClicked just do this: 然后在您的addBtnClicked执行以下操作:

- (IBAction)addBtnClicked:(id)sender {
    [tableDate addObject: addCityTF.text];
    [myTableView reloadData];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM