I a have 2 Buttons within my custom TableViewCell.
The Buttons are in bright colors. (BackgroundColor set)
However: Running the app - The Buttons color disappear and it becomes white:
I tried to programmatically change the color in viewDidLoad but Xcode doesn't react.
Any Ideas?
The heart of the problem is that you have introduced buttons but you are also setting the cell's textLabel!.text. You can't mix and match like that. If you're going to use a custom cell, you must use a completely custom cell.
Set the cell's type to Custom, drag a label into it, use a custom cell class, give it an outlet to the label, set this label's text, and all will be well.
Here's my custom cell with an outlet:
class MyCell : UITableViewCell {
#IBOutlet var label : UILabel!
}
Here's my cellForRow:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! MyCell
cell.label.text = "Hi" // NOT cell.textLabel!.text
return cell
}
As you can see, the result is that button appears just fine.
You have an extra word in there:
cell.noButton.backgroundColor = UIColor.greenColor()
Related
I need to place a radio button in tableview custom cell. whenever user clicks the tableview cell or button then radio button needs to work. I tried by using below code but didn't execute well.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:TableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! TableViewCell
cell.country.text = self.animals[indexPath.row]
cell.selectionStyle = UITableViewCellSelectionStyle.none;
if selectedRows.contains(indexPath)
{
cell.radioButton.setImage(UIImage(named:"check.png"), for: .normal)
}
else
{
cell.radioButton.setImage(UIImage(named:"uncheck.png"), for: .normal)
}
return cell
}
Here's a great solution for creating radio buttons in a UITableView using a storyboard that requires zero code - and has 2 great Cool Tips!!
Make sure your table view is set to Single Selection, and to use Static cells.
Add a Basic cell, set the image to be your unchecked button image, and make sure the selection style is Default
Cool Tip # 1: Click on and select the cell's image view, and then set it's highlighted image to be your checked state. When the cell is highlighted or selected, the image view within will change to show its highlighted state.
Cool Tip # 2: Next, drag a UIView into the cell's content view, behind the text label. As you're using a basic cell, you won't be able to drop it directly into the cell, you'll need to drag it into onto the Document Outline on the left instead. Then hook this up to the cell's selected background view outlet. When a cell is selected (or highlighted), this view will be displayed in the background. In this case, we're going to use it to prevent the grey background appearing, so set its colour to Clear. Note that it doesn't matter what size the view is, and there's no need to set any constraints - it's automatically sized to match the cell at runtime.
Finally, duplicate this cell and change the text for each of your radio button options. Build and run, and you have code-free radio buttons!
In your TableViewCell class why don't you create a data source element and override the didSet for it. also in your data source for the UITableView I would recommend an array of something more than just a String.
I haven't compiled the below so this is just an idea.
import UIKit
class TableViewCell : UITableViewCell {
var data: Animal? {
didSet {
self.country.text = data.description
if (data.isSelected) {
self.radioButton.setImage(UIImage(named:"check.png"), for: .normal)
} else {
self.radioButton.setImage(UIImage(named:"uncheck.png"), for: .normal)
}
}
}
}
in your view controller you will of course have to set the isSelected property whenever a row is tapped.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
var animal = self.animals[indexPath.row]
animal.isSelected = !animal.isSelected
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:TableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! TableViewCell
cell.data = self.animals[indexPath.row]
}
and for your Animal maybe something like this:
struct Animal {
var description: String
var isSelected: Bool
}
In my viewcontroller,
I added my tableview (#IBOutlet weak var MyTableView: UITableView!) in which I added several custom cells with specific size depending on the elements needed inside.
I call each cell with identifier :
func tableView(_ MyTableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = MyTableView.dequeueReusableCell(withIdentifier: "FirstCell") as! FirstCell
self.SettingsTableView.rowHeight = 220
return cell
} else if indexPath.row == 1 { etc etc...
And at a specific row, I've added a UIswitch directly in the cell that should expand / collapse this cell depending if it's ON or OFF.
I'd like to make it work a bit like the SelectRowAtIndexPath method and animate it to make appear 3 textFields below....
I'm looking for hours a solution, if you have any idea... Any suggestion is welcome !
If your switch is properly hooked up to the cell, the problem may be that you didn't refresh the UITableView, and thence it doesn't expand.
I've created a custom UITableView Cell and everything seems to be working with one exception. I have a UISwitch that is inside the cell (hooked up to it's own UITableViewCell class that the tableView loads) but it only appears when you click on the cell or the cells background is clear/transparent. Ideally I have a white background for the cell and the switch on top of the background.
I've tried some hacky stuff like:
cell.bringSubview(toFront: cell.switch)
and
cell.switch.isHidden = false
But that obviously didn't work.
The switch is enabled and ON by default.
The tableview and switch is created from storyboards.
The hierarchy looks like this - TableView > Cell > Content View > Switch
Here's a video to see in detail - http://quick.as/rpyub8mv
Xcode Storyboard Screenshot
Custom TableViewCell Class
class SettingsBoolCell: UITableViewCell {
#IBAction func switchAction(_ sender: UISwitch) {
}
#IBOutlet weak var switchOutlet: UISwitch!
}
ViewController Implementation
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "settingsSectionOne", for: indexPath) as! SettingsBoolCell
switch indexPath.section {
case 0: cell.textLabel?.text = titles[0]
case 1: cell.textLabel?.text = titles[1]
case 2: cell.textLabel?.text = titles[2]
default: ()
}
return cell
}
If you're using storyboard, make sure that your UISwitch is inside the right view, and that it is under the other components in your document outline.
If you're generating the view inside the cell programmatically, make sure that you add the UISwitch to the right view with addSubView last. You can also set zPositions with view.layer.zPosition attribute.
So I was setting -
cell.textLabel.text?
to change the text of the cells inside the tableView. The problem is, apparently when you are using a custom cell, you can't access the default cell properties without some funky behavior.
Thanks everyone for your help!
How can I achieve this screen with UITableViewCell and UITableViewController. With table section and header. Some ideas to achieve this?? Thanks!
What have you tried so far?
Your question seems a little broad.
You will need a set of custom UITableViewCell Subclasses, which you design in nibs.
To make the cells seem apart from each other, resize the content size of the Cells, and make the cell background another color.
Create a Segmented Control and add it to the Tableviews HeaderView.
For the FooterView it seems like this is some kind of subclassed Tabbar.
Easiest way to customise it in such a way, would be to create a View, and add buttons to it. Add this View as Subview to your TableViewController.
Have 2 UITableViewCell's one for each type i.e. 1 for showing the image and text and another for showing just the text.
Then in the cellForRowAt delegate method determine which to type to use based off the object you are data binding it to.
Example:
public final func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let customObject = customObjects[indexPath.section]
switch customObject.type {
case .imageAndText:
if let cell = tableView.dequeueReusableCell(withIdentifier: ImageAndTextCell.identifier, for: indexPath) as? ImageAndTextCell {
cell.customObject = customObject
return cell
}
case .text:
if let cell = tableView.dequeueReusableCell(withIdentifier: TextCell.identifier, for: indexPath) as? TextCell {
cell.customObject = customObject
return cell
}
}
return UITableViewCell()
}
I'm trying to make a simple UITableView that has a button appended to the right side of every cell. I added the button to the prototype cell in my storyboard and set up the constraints so that AutoLayout takes over and puts it in the right spot. When I go to preview the layout in the Assistant Editor, everything looks great. However, once I start the simulator and go to that view, the button is no longer there (the cell is blank).
I ran into a similar problem when I tried to add a Disclosure Indicator and it wasn't showing up - to fix it I had to add that accessory programatically. Isn't the point of storyboard to help make changes without using code? I'm still really new at this and would appreciate some guidance.
EDIT: Some code as requested (sorry):
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let addButton = UIButton()
let menuItem = currentOrder?.getSortedMenuItems()[indexPath.row]
let itemPrice = "\(currentOrder!.menu[menuItem!]!)"
cell.textLabel!.text = menuItem! + " - " + itemPrice
return cell
}
Most of the code in there is pretty irrelevant, but as you can see I tried adding a button and then stopped cause I wasn't sure where to go from there. I was under the impression that all of this could be done using storyboard.
The problem which you have arises from the fact that you are NOT using the cells which you created in your storyboard. When you call
let cell = UITableViewCell()
it creates a cell with default style. What you want to do instead is to use the cells which you created in the storyboard.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cellWithSwitch") as! UITableViewCell
// setup your cells (e.g. update its title)
return cell
}
You should change the "cellWithSwitch" to the identifier which you have set for your cell in storyboard.
You can find the identifier of your cell by selecting it in the storyboard and then checking info in the Attributes inspector:
If you haven't yet set this identifier - set it and then use it. And then changes which you make in storyboard will get reflected on the phone ;)
Extra
It is often necessary to subclass your cell to make it easier to work with. Here is an example:
class MyCell:UITableViewCell {
#IBOutlet var myButton: UIButton!
}
Now, you have to do 3 other things:
1) Select your cell in the storyboard and set its class to MyCell in Identity inspector (that's the third tab in the image which I posted)
2) Open Connections inspector (last tab) and connect the myButton outlet
3) Modify your cellForRowAtIndexPathFunction:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cellWithSwitch") as! MyCell
// setup your cells (e.g. update its title)
// you can now access your button as cell.myButton
return cell
}