To make UITableView's cell static.

Source: Internet
Author: User

UITableView provides us with a very good display list tool. There is a built-in multiplexing mechanism in which cells can be taken in a general way without creating a new one each time the cell scrolls. Achieve a very good user experience. However, sometimes we don't need to reuse in some scenarios. We will create all the cells we need and put them in the UITableView. The premise is that the number of cells to be displayed is very limited. For example, in an app's settings. The style shown is UITableView's grouped style, with the section header separating several different groupings. But all the cells are just showing some "push settings",
"Font settings", "Offline Settings" and "Empty cache". 10 fingers can be counted to come over.

The simplest way to achieve this is to use the Xib file. However, this year's discussion of Xib has become obsolete. It's all in storyboard. The controller of a scene specifies Uitableviewcontroller or inherits from Uitableviewcontroller. You can then drag it up to the cell and customize it, and so on. However, recently, because of the problem of the adaptation of the old project, so the use of Non-storyboard. This needs to be created directly using the code. There has to be a groove here, using storyboard do not necessarily have code to specify frame come fast!

You do not need to use Uitableviewcontroller or its subclasses if the code creates a static cell. Code is FREEDOM! Whether the cell is static or not, UITableView must be there. So our distribution introduction in the beginning or create a tableview.

Self.tableview = UITableView (Frame:cgrectmake (0false  false Self.tableview. delegate === = Uitableviewcellseparatorstyle.none        Scrollview.addsubview (Self.tableview)//I am here on the scroll view, you decide where to put

After that, as mentioned earlier. We need to create our own cell.

    var pushsettingscell:uitableviewcell!    // Other cells     here //  ...    var clearcachecell:uitableviewcell!

The key is in the method func tableView (tableView: UITableView, Cellforrowatindexpath indexpath: nsindexpath) -- UITableViewCell inside how to achieve. The general way to do this is to dequeue a cell first, and if it is empty, initialize a uitableviewcell and put the desired control on it (if you do not redefine a cell). Finally, return to this cell. However, this is not a static cell. Because, you do not know this method returns the cell is not the cell that you previously specified on this row. The static cell is the one that was originally created anyway. That is, the Pushsettingscell we created in the code above. So, this approach is like this:

Func TableView (Tableview:uitableview, Cellforrowatindexpath Indexpath:nsindexpath)UITableViewCell {varCell:uitableviewcell? =NilSwitch(indexpath.row) { Case 0: Cell=Pushsettingscell// ...            default: Cell=//another cell             Break; }
Return cell! }

So it's almost done. However, if you have a static uitableview that still contains the values you need to extract, there is still some trouble. Then refactor the code. Put the initialized cell in the array.

var celllist: [UITableViewCell]?

This can be very simple when extracting cells from the above method.

Func TableView (Tableview:uitableview, Cellforrowatindexpath indexpath:nsindexpath), UITableViewCell {    /c5>return celllist! [Indexpath.row]}

refered:http://derpturkey.com/create-a-static-uitableview-without-storyboards/

To make UITableView's cell static.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.