简体   繁体   English

如何构建我的iOS代码?

[英]How to structure my iOS code?

I have quite a complex setup, and am looking for advice in how to structure my code. 我有一个非常复杂的设置,并且正在寻找有关如何构建代码的建议。 What I'm trying to do is something similar to twitter when you press one of a users images and can swipe through them. 当您按下一个用户图像并可以在其中滑动时,我要尝试的操作类似于Twitter。 This is what I have at the moment: 这是我目前所拥有的:

  • A UIViewController subclass that gathers data from a server, and passes it to a UITableViewController subclass through a variable called theData 一个UIViewController子类,该子类从服务器收集数据,并通过一个名为theData的变量将其传递给UITableViewController子类。
  • This UITableViewController subclass also gathers more data as the users scrolls to the bottom of the table view and appends it to theData (ie uses pagination). 当用户滚动到表格视图的底部并将其追加到theDataUITableViewController子类还收集更多数据(即,使用分页)。 This UITableViewController subclass is reused throughout the app so that data from a different source can be passed to it. UITableViewController子类可在整个应用程序中重用,以便可以将来自其他来源的数据传递给它。
  • When a user clicks on a UITableViewCell , a UIWindow is created which has a UIPageViewController inside it. 当用户单击UITableViewCell ,将创建一个UIWindow ,其中包含UIPageViewController The window uses the UITableViewController as a delegate to access theData , and passes the theData from the delegate to the UIPageViewController in a variable called pageData 在窗口使用UITableViewController作为代表访问theData ,并传递theData从委托到UIPageViewController在称为可变pageData
  • Each page of the UIPageViewController is a UIViewController that loads images from the pageData UIPageViewController每个页面都是一个UIViewController ,它从pageData加载图像

The next stage is to do pagination as the user swipes to the end of the pageData in the UIPageViewController . 下一步是在用户滑动到pageDataUIPageViewControllerpageData时进行UIPageViewController However I don't think my code is structured well enough to reuse the pagination code from the UITableViewController . 但是,我认为我的代码结构不够好,无法重用UITableViewController的分页代码。 Please could someone give me advice on how to improve this code structure? 请有人可以给我有关如何改进此代码结构的建议吗?

Thank you 谢谢

EDIT 编辑

The UIViewController in the first bullet point does more than just downloading data (it has other views within it). 第一个要点中的UIViewController不仅仅是下载数据(它具有其他视图)。

I use a UIWindow to encapsulate the UIPageViewController because I want it to expand from the frame of the UITableViewCell and "take over" the full screen - covering the status bar. 我使用UIWindow封装UIPageViewController因为我希望它从UITableViewCell的框架扩展并“接管”整个屏幕-覆盖状态栏。

My advice would be to create simple service which you can use for communication with the server. 我的建议是创建可用于与服务器通信的简单服务。 You don't need View controller for that. 您不需要为此的View控制器。

Further more you can create static methods that will give you theData without passing it all over the application. 此外,您可以创建静态方法,这些方法将为您提供theData而无需将其传递给整个应用程序。

Regarding the swipe, you can take 3 users in an array, and corresponding to scroll, load additional one, and release unnecessary one. 关于滑动,您可以在一个阵列中容纳3个用户,并且对应于滚动,加载其他用户和释放不必要的用户。 That would help you with memory usage ( if users swipes through 50 or more users ). 这将有助于您使用内存(如果用户在50个或更多用户之间滑动)。

Instead of UIWindow , create simply segue from UITableViewController to DetailView, or in your situation, UIPageViewController . 代替UIWindow ,只需创建从UITableViewController到DetailView的连接,或者根据您的情况UIPageViewController

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM