简体   繁体   中英

Best way to simplify/refactor tableView code setup in objective-c

Every single time I need to create a simply tableview that is populated by a simple data set retrieved from my web server which has its code executed like this: SELECT * FROM table I find myself spending two blady whole hours trying to get the new view controller up and running as I try to update some variable names, copy and paste the required code from my previous view controllers. etc its ridiculous.

This is the end result for all my view controller pages where each will contain different data sets depending on the web service url being called:

最终结果是什么样的

Here is a link: Link to downloading staple code .h .m and .xib files

This view controller contains a few simple elements seen throughout all data viewing pages:

  • UITableView
  • Titled header views
  • table indices.
  • refresh table control feature
  • data connection retrieval code
  • data connection succeeded
  • data connection failed
  • setting up all my bloody delegate and data source methods.

I find myself having to copy and paste all the staple code, functions, variables, properties, and IBOutlets; and to be frank, its getting ridiculously paintaking to have to repeat the same procedure over and over again but changing variable names between the different view controllers.

This is why I believe people create simple component like structures that make it easy for users to get tables setup and up and running.

How can I reduce this big chunk of code:

to something that will allow me at most do this:

  • Create a new view controller
  • Setup xib file
  • create appropriate IBOutlets, and hook them up to the xib.

Here's where it needs to change

I need to now simply able to write something like this the next time I am goin to create another data viewing View Controller:

[self setupTableForDataSetType:]; //This will make sure the tableView knows which data set its dealing with and so therefor know which DataModel classes to use

[self retrieveDataWithWebServerURL:]; //of course so that the connection code can make the right server connection with the URL given for the data set required.

Thats it. So that it is super easy for me to create the tableView pages desired and show the results quickly! Atm I have the same code everywhere in different view controllers.

Whats the best way to go about doing this?

创建一个具有所有可自定义值作为属性的viewcontroller,并重复使用更改其值的方法。

Well, subclassing is probably the best (maybe only) way. I've done something like this for tables with an index, since they're a bit of a pain to set up. I created a IndexedTableViewController that handles almost all the load. I make my app table view controller a subclass of that controller, and then I only need to feed a simple array of custom objects to the method, convertArray:usingSectionKey:secondarySortKey:(implemented in the IndexedTableViewController) which creates the sections and the index. The only other method I have to implement in my app table view controller is cellForRowAtIndexPath:(though I would have to implement more, especially didSelectRowAtIndexPath:, if I were doing more things with this table).

Your needs sound a bit more ambitious than this, so it would take quite a bit of work to make a superclass that would be general enough to work with most of your apps. A method like setupTableForDataSetType: could be quite complicated if it needs to handle many different data types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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