Template for UITableViewCell without nib files or storyboard - ios

I want to separate the code decide how the cell display from UITableViewCell class for reuse or switch template in runtime. Like this:
-(void)setTemplate:(MyTemplate *)template {
[self.productName setStyleFrom:template.mainLabel];
....
}
And I don't want to use nib files or storyboard, code only.
Thank you.

You can use Prototype Cell if you Don't want to add Cell through .xib or class which is derived from UITableview cell
Click the link for Prototype cell http://www.raywenderlich.com/50308/storyboards-tutorial-in-ios-7-part-1

Related

Using a custom UITableViewCell with different custom classes

I have a xib file named MyCell.xib with a custom UITableView cell and MyCell.swift where I have outlets and code that sets constraints.
Now I want to use that xib file in a different UITableView and have MyNewCell.swift inherited from MyCell.swift in order to override the code that set constraints. However, I don't know how to do it because in my xib I can only specify one class as a custom class for my cell. And when I get a cell in cellForRowAtIndexPath I get an object of type MyCell not MyNewCell.

Connecting label to UITableViewCell

I've created several tables in the past but I believe it's something with the way I'm doing it that Xcode doesn't like this time.
I have several xib files with a UIView inside. These together creates a slideview like snapchat.
In one of these xib files I have a UIView. In this I have a UITableView and a UITableViewCell. I have set up a prototype cell with an identifier "cell". I've set up the delegate and dataSource as I usually do.
I made a UITableViewCell class and set up the class for the prototype cell.
Here's where the trouble is:
I wanna connect my labels on the cell to the UITableViewCell class and make IBOutlets. But Xcode doesn't let me do it. It simply doesn't connect them.
Any suggestions on why I can't do it or if there's a way around??
Change class of your UITableViewCell to your custom class in xib. (in utilities section,there is option called custom class)

iOS Table cell styling

In my iOS - swift project I need my table cells to look like this. I can get the multi select option by making the table editable. What I need to know is what are the ways that I can design a cell to look like this? How can I add that colored border at the bottom and how can seperate the cells to look like this?
Thanks
The cells you show in your question are custom cells. To make that, you would need to design those cells yourself with views, labels, buttons, etc. There is no default/boilerplate code that can do this for you; you will need to implement it yourself.
You must create and implement this custom cell.
Create a New file, Cocoa Touch class, on next page select "subclass of: UITableViewCell" and name "CustomCell"
Create a New file, select User Interface (under iOS) and select Storyboard, and Name something like "MyCustomCell". On this new storyboard drag a Table View Cell. Select this cell, and in identity inspector make the class = to the name of your "CustomCell" cocoa touch class file. Then go to Attributes Inspector and fill in "identifier" with "cell"
NOW
In your TableViewController file
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: CustomCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! CustomCell
// Here's where you set your CustomCell properties like label title, images, backgrounds
return cell;
}
I don't know yet how to make the cells separated, but as to the border at the bottom just go to your "MyCustomCell" xib and add a view at bottom and change its background.
Maybe to get the space in-between you can use some sort of Layer (CAlayer).

How to reuse custom UITableViewCell defined as a prototyped cell in Storyboard controller

I have the only cell template for items at two different UITableViewControllers/TableViews.
What I need is to define it once and then reuse at other UITableView via
UITableView.DequeueReusableCell(CellId);
The issue is that is when I call this method on UITableView which doesn't contain cell prototype I'm getting NULL.
How to reuse my prototyped cell across multiple table controllers?
I want to define cell template in storyboard, NOT xib.
It turned out that the only way to reuse a cell it to design it with xib and register at tableview that xib with cellid.
Just copy-paste your prototype cell in every table view controller where you need it.
And if I understand your question, in a standard and proper way, it's not possible to dequeue a cell from another table view, Apple implementation handles this mechanism itself.
Using xib for a reusable cell is beneficial while cell design is fixed in the whole app. But when there are conditional requirements or slight changes in design or functionality and remaining design and functionality is same for tableview cell, in this case if you still want to reuse the code then you can subclass tableview cell class.

How do you create a custom prototype cell with master detail view in Xcode?

I created an xcode project with the master detail template. I want to customize the cell to put my data into 2 separate labels. I tried customizing the cell in the storyboard editor, but it seems like all of the stuff on the cell is locked in place. I am fine either creating the custom cell programmatically or in the storyboard editor. I know I could just set the text on the default textLabel to blank and then create the other labels programmatically, but that doesn't seem very need to just have a random empty textLabel in the middle of every cell. So I am wondering if there is a way to edit the cell in storyboard editor, delete the default textLabel, or resize and reposition the textLabel to where I need it to go.
Any help is greatly appreciated.
Thanks in advance.
That's standard practice. Just ignore the built in textLabel and be done with it. By default the label is blank anyway. It's set in code.
To access new labels and such, create a new custom class for the custom cell (File|New|File...|Objective-C Class in XCode), give it a name and for the subclass choose UITableViewCell. Finally, on the Identity Inspector for the custom cell in Interface Builder, choose that new class for the Custom Class.
Now, from the Assistant Editor in XCode, you can CTRL-drag new labels and such to the .h file of the new custom cell class to create IBOutlet properties.
Be sure to set the cell Identifier on the Attributes Inspector tab for the cell and reference that in your code, especially in cellForRowAtIndexPath.
Import the custom cell class's .h file in the view controller's .h file, then cast the cell to your custom cell class in cellForRowAtIndexPath to access the new properties or change the definition UITableViewCell *cell = to your new class like MyTableViewCell *cell =.
By default, the Style of the table cell is "Basic" in Master-Detail projects. Select the Table View Cell and open the Attributes Inspector. Change the Style from Basic to Custom.
Doing this removes the default label and allows you to add new controls.

Resources