gpt4 book ai didi

ios - UITableViewController 与 UITableView

转载 作者:行者123 更新时间:2023-11-29 13:37:28 25 4
gpt4 key购买 nike

我在一个应用程序中有 2 个 Controller ,因为我正在尝试这些不同的设置。

一个是 UITableViewController,据我所知,它的委托(delegate)和数据源是从父类(super class)中提取的。

现在按照这个理论,我现在也有一个带有 UITableView 的 UIViewController,并且在头文件中我已经声明了数据源和委托(delegate)。

到目前为止一切顺利:-)

现在这是我不明白的一点:

在实现文件中,我可以使用 tableView 指针链接数据,但我使用的是 Core Data,并且我有这段代码:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView beginUpdates];
}

如果我不设置一个 UIViewController,这种情况和其他 5 种情况都会出错

IBOutlet UITableView *tableView;

当然是合成了:-)

但是如果我声明这个 tableView,我会在这样的行上收到一个双倍警告:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}

因为它还声明了一个tableView。

所以我有两个问题:

1) 如果我已经设置了 UITableViewDatasource 和 Delegate,为什么还必须设置 IBOutlet?

2) 如果我给 UITableViewCell 一个不同的指针名称——比如 tableView2,为什么我在编辑数据时看不到变化,而只能在重新启动应用程序后看到结果?

MasterViewController,也就是UITableViewController没有这个选项。

干杯杰夫

---添加图像----

enter image description here

enter image description here


为了进入编辑模式,我在 viewDidLoad 中有这段代码

self.navigationItem.leftBarButtonItem = self.editButtonItem;

然后我使用此代码来执行需要完成的操作:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{ 如果(editingStyle == UITableViewCellEditingStyleDelete){ NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext]; [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];

    NSError *error = nil;
if (![context save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}

现在 + 选项有效,但我只有在重新启动应用程序后才能看到结果。我可以滑动和删除,但它不会在应用程序的同一实例中实时动画。

最佳答案

您已经声明了一个名为 tableView 的属性,并且可能只是用相同的名称合成了它。

UITableViewController 也有一个名为 tableView 的属性,但是 backing ivar 将被称为不同的东西,可能是 _tableView,正是因为默认的 tableview 数据源和委托(delegate)方法通常传入一个名为 tableView 的参数,在方法的范围内,它与您的实例变量发生冲突。

在该方法中,tableView 是什么意思?传入的参数,还是你的 ivar?

回答您的具体问题:

  1. 当您的 View Controller 需要与 TableView 对话时(例如当您告诉它开始更新时),您需要这些实例的导出。当 TableView 需要与您的 View Controller 通信时(比如当它请求一个单元格时),数据源和委托(delegate)连接就在那里。

  2. 我不确定您在这里问的是什么,您可以随意调用 TableView 的导出,但您还必须更改向 self.tableView 发送消息的所有内容。为了一致性,我会像这样合成属性:

    @synthesize tableView = _tableview;

    这首先阻止了名为 tableView 的 ivar 存在。我假设您还没有声明 ivar,并且只将该属性声明为 IBOutlet?:

    @property (nonatomic) IBOutlet UITableView *tableView;

关于ios - UITableViewController 与 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10188179/

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