Ios 添加行后如何更新UITableView?
我有一个非常简单的视图。我在这里初始化它,创建一些对象并将它们添加到tableview中,这非常有效:Ios 添加行后如何更新UITableView?,ios,objective-c,iphone,uitableview,xcode5,Ios,Objective C,Iphone,Uitableview,Xcode5,我有一个非常简单的视图。我在这里初始化它,创建一些对象并将它们添加到tableview中,这非常有效: - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; return self; } - (void)viewDidLoad { [super viewDidLoad]; //Instantiate lapTimerModel = [[LTM
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//Instantiate
lapTimerModel = [[LTModel alloc] init];
[lapTimerModel addChallenge:@"I made it"];
[lapTimerModel addChallenge:@"Say the alphabet"];
[lapTimerModel addChallenge:@"100 Meter Sprint Challenge"];
[lapTimerModel addChallenge:@"Read the csci342 al spec challenge"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [lapTimerModel numberOfChallenges];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
LTChallenge *challenge = [lapTimerModel challengeAtIndex:indexPath.row];
cell.textLabel.text = challenge.name;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
现在我定义了一个按钮,我想向表中添加另一行,我创建了一个新对象并添加到其他对象的列表中,但它没有添加到TableView?我想我必须再打个电话,扔掉我的物品清单,并带上新的
- (IBAction)newAlert:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Enter Name"
message:@" "
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
NSString *name = [alertView textFieldAtIndex:0].text;
NSLog(@"%@",name);
//Create a new challange
[lapTimerModel addChallenge:name];
//[self.tableView reloadData]; Not working?!
}
}
@end
我可以在控制台中看到我在警报中键入的内容。检查这些:
希望这有帮助:) 您应该为lapTimerModel创建一个iVar
@property (strong, nonatomic) LTModel *lapTimerModel;
并在所有UITableViewDelegate函数中调用self.lapTimerModel
您可以调用
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
NSString *name = [alertView textFieldAtIndex:0].text;
NSLog(@"%@",name);
//Create a new challange
[self.lapTimerModel addChallenge:name];
//[self.tableView reloadData]; Not working?!
[[self tableView] beginUpdates];
[[self tableView] reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
[[self tableView] endUpdates];
}
}
验证self.tableView未被禁用。检查在添加初始数据集之后,是否在数据结构中添加了数据。能否显示LTChallenge/LapTimerModel的类文件代码?我已经使用一个标准的NSMutableArray进行了测试,它工作得很好,所以我不得不假设这与在重新加载数据之前向自定义mutableArray类添加对象的延迟有关