重新加载TableView无法正常工作

时间:2014-10-15 02:53:07

标签: ios objective-c uitableview

我有一个用户进入城市的文本字段以及用于填充用户选择的城市的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];
    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];
}

enter image description here

1 个答案:

答案 0 :(得分: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];
}

希望这会有所帮助.. :)

修改

更好的方法是不要使用刺痛,只需使用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];
}