gpt4 book ai didi

ios - 重新加载 TableView 不起作用

转载 作者:行者123 更新时间:2023-11-28 21:56:24 24 4
gpt4 key购买 nike

我有一个用户输入城市的文本字段,还有一个表格 View 来填充用户选择的城市。但是,一旦用户添加了一个新城市,我的表就不会重新加载,因此数据不会在 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

最佳答案

你没有添加 ',' 而附加了另一个字符串和错误的字符串来制作数组,我认为这是问题所在:

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

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

编辑:

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

关于ios - 重新加载 TableView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26373723/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com