I am new to StackOverflow
, and have been struggling with this issue for a while, really hoping someone out there can help and point me in the right direction.
I am trying to make a custom UITableView
and a custom UITableViewCell
- they will both go together to form a custom UITableView
which I would like to reuse across multiple projects. So what I am trying to do is encapsulate all row + keyboard handling etc in the custom UITableView
, and the cell's look in the custom UITableViewCell
.
In a new StoryBoard, I added a UIViewController
and added a UITableView
, designed how I wanted the table to look and added a prototype cell to it. I designed this with the appropriate labels, fields and constraints. Lets call this custom look table "MyCustomTableWithCustomCell"
So now I have my custom table and what I want is to reuse this across other Storyboards.
I have found one solution (thanks stackoverflow!), but it isn't exactly what I wanted:
MyCustomTableWithCustomCell
(cut and paste into an Empty Interface Builder document) and saved it as a xib. UITableView
via registerNib() UIViewController
in any Storyboard, add a UITableView
, set its class to MyCustomTableWithCustomCell
and it will do what I want The problem is, I don't like how the cell is in a separate xib file, as it can't be previewed easily across different device sizes via IB auto-preview. Regardless of which device size I select, it always looks the same. It is also hard to see how the design fits with the rest of the screen unless I build and preview in Simulator every time I want to change anything. I'd prefer if the cell design could remain as part of the Prototype TableViewCell inside the blueprint Storyboard. Then I could easily modify it, preview across different device sizes etc.
So my question is, what is the best way to do the same thing, but instead of using a xib file, using a Prototype Table Cell from a Storyboard as the blueprint. Or in other words, is there a way to extract the Prototype cell into a Nib at runtime so that I can register it when I create my custom table.
Eg. something like:
var myTableCellNib = myStoryBoard.myViewController.myTableView.myPrototypeCell.getNib()
registerNib(myTableCellNib)
Look forward to hearing your thoughts!
Make UITableViewCell
Swift file -
import UIKit
class ClassName: UITableViewCell {
@IBOutlet weak var lblData: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Go to storyboard - > View Controller -> Table View -> Drag and drop table view cell and design it, give class name as ClassName and identifier.
Go to cellForRowAtIndexPath
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell : ClassName! = tableView.dequeueReusableCellWithIdentifier("identifier") as! ClassName
cell.lblData.text = ""
return cell as ClassName
}
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.