- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在重建一个需要显示项目列表的应用程序。此列表使用 API 检索或从 Core Data 检索并显示在 UITableViewController 中。我遇到的问题是已经有七个不同的列表,其中差异很小。它们中的大多数只是不同的项目,但也包括一个列表中的搜索栏,并且之前声明的一个列表不会使用 API 加载,而是从 Core Data 加载。
在我的 Storyboard 中,我添加了一个 UITableViewController,其类 ItemsTableViewController 具有设计好的 UITableViewCell。我已经为这个单元格添加了一个标识符,所以我可以在这个 View Controller 中重用它。从主屏幕到此 View Controller 有一个 segue。
我们的想法是创建一个父对象 (ItemsTableViewController) 并添加多个子对象(SavedItemsTableViewController、LocalItemsTableViewController 等),它们都将使用父对象的共享逻辑,只有一些小的更改(API 和一些自定义内容)。
我目前的工作正常,但没有子对象:
- (void)offlineButtonPressed {
[self performSegueWithIdentifier:@"openItemsTableViewController" sender:@(ItemListOffline)];
//[[self navigationController] pushViewController:[[OfflineItemsTableViewController alloc] init] animated:YES];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"openItemsTableViewController"]) {
switch((ItemList)[sender unsignedIntegerValue]) {
case ItemListOffline: { [(ItemsTableViewController *)[segue destinationViewController] retrieveOfflineDocuments]; break; }
case ItemListSearch: {
[(ItemsTableViewController *)[segue destinationViewController] retrieveDocumentsWithQuery:@""];
[(ItemsTableViewController *)[segue destinationViewController] addSearchBar];
break;
}
// Loop through all the list...
default: { NSAssert(NO, @"Unhandled type of document list."); break; }
}
}
}
所以应用程序只是调用处理请求的父对象的一个函数。但正如您可能感觉到的那样,该对象的可维护性会降低。在评论中,我推送了 subview Controller ,但由于它不包含 Storyboard View ,我需要从头开始重新创建单元格,这感觉不太好。此外,由于重用标识符,它无法正常工作,因为它无法使具有在 tableView:cellForRowAtIndexPath: 方法中设置的标识符的单元格出队。
现在我想知道最好的做法是什么?似乎我不能将 Storyboard中的保存 UITableViewController 用于多个子类,可以吗?但是在 Storyboard 中创建七个(将来可能会更多) Controller ,我需要将单元复制到每个 Controller 并只给它们不同的类,这似乎不是可行的方法。并向父级添加一个方法,在该父级中将以不同方式检索列表并更改一些内容(例如添加搜索栏),但也不是看起来最好的方式。
更新:ItemsTableViewController 中的逻辑非常简单。在委托(delegate)和数据源中,我处理的文档几乎相同。按列表类型检索的方法类似于:
- (void)retrieveOfflineItems {
[self startLoading];
[[APIManager instance] getOfflineItems:^(NSArray<ItemList *> *list, NSError *error) {
[self setDocuments:list];
[[self tableView] reloadData];
}];
}
但是还有更多的东西,比如搜索必须添加一个搜索栏(一旦加载了 View )。所以在执行segue的时候需要调用多个方法。
最佳答案
您可以拥有一个 UITableViewController
,其中包括所有 您可能的表格单元格,并且作为 prepareForSegue
调用的一部分,您应该为 Controller 和数据设置类型标识符 - 无论数据来自何处。
在 UITableViewController
类中,您可以隐藏/显示此数据类型所需的功能 - 例如搜索栏,并在 tableView 方法中,选择您需要的数据源。
这样,您需要维护的一个 UITableViewController
类比一个专用类稍微复杂一点,但比 7 个或更多类更易于维护!
假设您已经设置了一个变量 dataType 来标识您需要的数据类型,您可以为 numberOfRowsInSection
设置类似的内容,然后为其他 tableView 方法设置类似的内容
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
switch self.dataType {
case dataType.MyFirstDataType:
return myFirstDataTypeArray.count
case dataType.MySecondDataType:
return mySecondDataTypeArray.count
case dataType.TheCoreDataType:
return myCoreDataArray.count
default:
break
}
}
关于ios - 重用 Storyboard View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41758769/
我在 iOS 项目中创建了一个新目标并添加了一些我的文件以及一个新的 Storyboard文件。但是当在模拟器中运行时(我还没有尝试过该设备)它会吐在 UIApplicationMain(argc,
The document "Storyboard.storyboard" could not be opened. Could not read archive. Please use a newer
我的 .travis.yml 文件: language: objective-c script: xctool -project ProjectName. xcodeproj -scheme
我正在使用 Storyboard,因为我有一个定制的表格。我需要在单元格内的标签上显示值。我尝试为它创建一个 IBOutlet,但它似乎不接受它,它给了我一个错误消息,“连接“xyz”不能将原型(pr
Xcode 6 界面生成器默认有新的复选框“使用大小类别”。它使 View 具有适应性。 当我尝试在 Storyboard中的两个 View 之间进行连接时,我有新的选择: 代替旧的: 现在我们有“s
我在新的 xcode beta 版本中打开我的项目时犯了一个错误 现在我无法用稳定版本打开它,因为 The document “Main.storyboard” requires at least X
如果我有一个已经使用 Storyboard 的 Xcode 项目,现在我想切换到以编程方式加载 View Controller ,我该怎么做? 最佳答案 这是一个in-depth article关于这
[错误]:**构建失败** [错误]:以下构建命令失败: [错误]:编译Storyboard LaunchScreen.storyboard [错误]:(1 次失败) 如何解决这个问题?它不会在 io
我试图找到一种方法来处理多个目标(从一个项目部署多个应用程序),我不知道如何定义UIColor并在 Storyboard 中使用它(我可以为UIImage做得很好)。我的想法是使用宏根据目标切换此颜色
我有一个在 Xcode 5.1 中构建的带有 2 个 Storyboard的项目。 当我在 Xcode 6 中打开其中任何一个时,我收到此错误。他们从未在任何测试版中打开过,所以我认为这不是 Xcod
我想将 StoryBoard 添加到以编程方式制作的项目中,是否可以这样做。 最佳答案 是的,您可以向 Xcode 项目添加新的 Storyboard。我正在分享以下屏幕截图,请检查: 您可以为该 S
我将几个配置文件分成多个 Storyboard以尝试帮助我减少卡住时间,因为我听说许多 Storyboard可能是 Xcode 有时会“卡住”长达 10 分钟的原因,有时会导致我的流程崩溃工作的。所以
我是 iOS 开发新手,我知道这是一个非常简单的问题,但我真的无法理解原因。 当我构建 SingleViewApplication 时,Xcode 自动生成 LaunchScreen.storyboa
为什么 Storyboard.SetTargetName 有效而 Storyboard.SetTarget 无效?这里 xaml -
我使用的是 Xcode 7.1,我正在使用 Storyboard 引用转到不同的 Storyboard 。问题是每次我运行模拟器时,都会出现一个错误,提示 “没有找到从 Main.storyboard
因此,我开始在XCode 4.3.2中使用 Storyboard 。我从Master-Detail应用程序(iPad的拆分 View 应用程序)开始。在详细信息 View (名为DetailVC)中,
我想在我的应用程序中加载辅助 StoryboardTest.storyboard(在Main.storyboard旁边),如下所示: let storyboard = UIStoryboard(nam
我有情节提要,其中假设Controller1有按钮 当我们按下按钮时,它应该移至下一个控制器,但是我无法访问它。能告诉我如何访问第二个Storybaord,其中一个名为Profile的TabbarCo
这是我在尝试启动主 Storyboard时看到的内容,并且 launchscreen.storyboard 也遇到同样的问题。 当我尝试访问 main.storyboard 时,我看到的是 XML 代
我在 main.storyboard 的 View Controller 中有一个 label1我在 Storyboard的 View Controller 中有另一个名为 Second.storyb
我是一名优秀的程序员,十分优秀!