One of IOS development cell customization methods

Source: Internet
Author: User

One of IOS development cell customization methods

The first method is to directly write an inherited UITableViewCell. Then add the subview dynamically.

Procedure:

1. Create a new cell class that inherits UITableViewCell

2. Define attributes in this class, such as UILabel

3. Reload the constructor to add the Sub-View

4. You can use this class.

CityCellTableViewCell class:

 

/// CityCellTableViewCell. swift // UITableViewDemo0 /// Created by Wang Feng on 7/27/15. // Copyright (c) 2015 wangfeng. all rights reserved. // import UIKitclass CityCellTableViewCell: UITableViewCell {var cityLabel: UILabel? Var cityTextFiled: UITextField? Var citySwitch: UISwitch? Override init (style: UITableViewCellStyle, reuseIdentifier: String ?) {Super. init (style: style, reuseIdentifier: reuseIdentifier) // initialize the sub-view and sub-control, and then add it to the current view cityLabel = UILabel (frame: CGRect (x: 5, y: 5, width: 80, height: 40) cityTextFiled = UITextField (frame: CGRect (x: 90, y: 5, width: 80, height: 40) citySwitch = UISwitch (frame: CGRect (x: 200, y: 5, width: 80, height: 40) // Add to current view self. addSubview (cityLabel !) Self. addSubview (cityTextFiled !) Self. addSubview (citySwitch !)} Required init (coder aDecoder: NSCoder) {fatalError (init (coder :) has not been implemented)} override func awakeFromNib () {super. awakeFromNib () // Initialization code} override func setSelected (selected: Bool, animated: Bool) {super. setSelected (selected, animated: animated) // Configure the view for the selected state }}

ViewController reload tableView method:

 

 

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{        var cellId = mycell        var cell:CityCellTableViewCell? = (tableView.dequeueReusableCellWithIdentifier(cellId) as? CityCellTableViewCell)                if(cell == nil){             cell = CityCellTableViewCell(style: UITableViewCellStyle.Subtitle,                reuseIdentifier: cellId)        }        cell?.cityLabel?.text = cities[indexPath.row]        cell?.cityTextFiled?.placeholder = input number        cell?.citySwitch?.on = true        return cell!    }


 

 

Related Article

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.