ios - 以编程方式将 UITableViewCell 添加到 UITableView

标签 ios uitableview

我有一个应用程序可以重新排列 UITableViewCells 的顺序并可以删除单元格。但是,我需要帮助在按下按钮时将新单元格添加到 UITableView 以及保存重新排列的单元格顺序的方法。

这是ViewController.m文件中的代码

- (void)viewDidLoad
{
[super viewDidLoad];
if (!maTheData) {
    maTheData = [[NSMutableArray alloc] initWithObjects:@"Bus", @"Truck", @"Car",nil];


}
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [maTheData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath           *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tvcItems"];
UILabel *lblName = (UILabel *)[cell viewWithTag:100];
[lblName setText:[maTheData objectAtIndex:[indexPath row]]];
return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:    (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source
    [maTheData removeObjectAtIndex:[indexPath row]];
    // Delete row using the cool literal version of [NSArray arrayWithObject:indexPath]
    [tableView deleteRowsAtIndexPaths:@[indexPath]   withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
    // Insert something into the array, and you can just add a populated NSIndexPath of   data or simplr reload data
    [maTheData addObject:@"I'm a new item!"];
    [tableView reloadData];
}
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
NSString *mover = [maTheData objectAtIndex:[fromIndexPath row]];
[maTheData removeObjectAtIndex:[fromIndexPath row]];
[maTheData insertObject:mover atIndex:[toIndexPath row]];

[tableView setEditing:NO animated:YES];

}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView setEditing:YES animated:YES];
}

最佳答案

在您的 IBAction 方法(连接到您的按钮的方法)中,您只需向 maTheData 添加一个新条目并调用 [self.tableView reloadData]

- (IBAction)addAction:(id)sender {   
  [self.maTheData addObject:@"A string object"];
  [self.tableView reloadData];
}  

关于ios - 以编程方式将 UITableViewCell 添加到 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16941082/

相关文章:

ios - 使用 itms-services URL 的 iOS8 应用程序无线 (OTA) 安装失败

ios - 从数组中的字典中获取数据并将其放入自定义单元格

iOS AVPlayer : pause video when screen locks

ios - 在 Swift 3 中处理模态视图 Controller 的关闭

ios - TableView 在 Storyboard 中卡住了第一响应者并退出

ios - CLLocationManager 返回 Nil 值

ios - 滚动 TableView 时 UISwitch 重置

iphone - UITableView单元格高度设置生命周期

ios - UITableViewCell 如何检测用户向左或向右滑动?

swift - NSInternalInconsistencyException : Invalid Update using tableview CoreData