I'm trying to create a custom cell that will only be applied to one single CELL in a Section.
So a create a custom cass called buttonsTableViewCell, that is pretty much empty and only have one label called weightLabel() code bellow:
class buttonsTableViewCell: UITableViewCell {
#IBOutlet weak var weightLabel: UILabel!
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
}
}
On the Main.storyboard i connected everything:
Now this is what i'm trying to do in the viewcontroller class, that controls the Tableview that has the cell.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = "Testing"
if indexPath.row == 1 { // So it only changes this cell
cell = tableView.dequeueReusableCellWithIdentifier("buttonsCell", forIndexPath: indexPath) as buttonsTableViewCell
cell.weightLabel?.text = "Weight" // ERROR UITableViewCell does not have a member named 'weightLabel'
}
return cell
}
What i'm doing wrong? Thanks in advance for the help.
Swift is statically typed language with type inference. So when you first assign the value of cell outside your if condition, the type of the cell variable is set to a normal UITableViewCell. Just use a different variable name like
if indexPath.row == 1 {
var buttonCell = tableView.dequeueReusableCellWithIdentifier("buttonsCell", forIndexPath: indexPath) as buttonsTableViewCell
buttonCell.weightLabel?.text = "Weight"
return buttonCell
}
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = "Testing"
return cell
Related
i want to make Swift app with CollectionView in Xcode, i have implemented it well and now i want to show some other stuff after some Posts, and i have created Xib and swift file for it. but the problem is when i try to show label in new cell i got this error ,,
i have registered new cell in ViewDidLoad Like this:
collectionView.dataSource = self
collectionView.delegate = self
collectionView.collectionViewLayout = UICollectionViewFlowLayout()
collectionView.register(AdViewCell.self, forCellWithReuseIdentifier: "adCell")
and i have used 300X250 space in AdViewCell and when i load posts i can see the white space of 300X250 size.. look at this
but even when i try to change its background color or anything , nothing happens when app is loaded..
i have connected Label to AdViewCell also..
class AdViewCell: UICollectionViewCell {
#IBOutlet weak var namelbl: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
please help
Your are using "cell" and "adcell" for the same indexPath.
try this:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item % 5 == 1 {
let adcell = collectionView.dequeueReusableCell(withReuseIdentifier: "adcell", for: indexPath) as! AdViewCell
return adcell
}else{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MovieCollectionViewCell", for: indexPath) as! MovieCollectionViewCell
return cell
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let indexPath = tableView.indexPathForSelectedRow() //optional, to get from any UIButton for example
let currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell
print(currentCell.textLabel!.text)
I'm lost. I searched and searched and cannot find the reason why my custom cell isn't displayed.
// ProfileCell.swift:
import UIKit
import QuartzCore
class ProfileCell: UITableViewCell {
#IBOutlet weak var profileNameLabel: UILabel!
#IBOutlet weak var profilePictureView: UIImageView!
}
The second default TableViewCell is displayed normally. I have no missing constraints in Interface Builder or any Errors. ProfileCell is selected in the ProfileCell.xib identity tab as the custom class.
// MoreTableViewControllerIB.swift
import UIKit
class MoreTableViewControllerIB: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// this cell is missing
if indexPath.row == 0 {
tableView.register(UINib(nibName: "ProfileCell", bundle: nil), forCellReuseIdentifier: "ProfileCell")
let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileCell", for: indexPath) as! ProfileCell
cell.profileCommentLabel.text = "Test Comment Label"
cell.profilePictureView.image = UIImage(named:"profile_picture_test")
return cell
// this cell is displayed perfectly
}else if indexPath.row == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "statisticsCell") ?? UITableViewCell(style: .default, reuseIdentifier: "statisticsCell")
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
cell.textLabel?.text = "Statistics"
cell.imageView.image = UIImage(named:"statistics")
return cell
// has to return a cell in every scenario
}else{
let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
return cell
}
}
}
Here is a screenshot of what I get.
tableView.register(UINib(nibName: "ProfileCell", bundle: nil), forCellReuseIdentifier: "ProfileCell")
add this line in viewDidLoad or viewWillApppear
So I found out what my mistake was. Pretty stupid and it cost me half a day:
The cell was already displayed, but the default height wasn't big enough to see it. I thought the set height in the .xib would be used. It apparently is not.
So I added this:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 192 // the height for custom cell 0
}
}
In my case I had to additionally register nib with view of my cell:
myTableView.register(UINib(nibName: "nibwithcell", bundle: nil), forCellReuseIdentifier: "cell") // you need to register xib file
I've tried multiple things to try and get a simple title and message table cell in Swift 3. This is the furthest and closest I've gotten for it to work. I am getting a Thread 1: Signal SIGABRT error on the first line of my App Delegate file and libc++abi.dylib: terminating with uncaught exception of type NSException in my console output.
Cell Class. The outlets are connected to the prototype cell in my storyboard file.
import UIKit
class MyTableCell: UITableViewCell {
#IBOutlet weak var label1: UILabel!
#IBOutlet weak var label2: UILabel!
}
Code to load table
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MyTableCell
if cell == nil {
let cell:MyTableCell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! MyTableCell
cell.textLabel?.text = posts[indexPath.row].title
cell.detailTextLabel?.text = posts[indexPath.row].message
return cell
} else {
cell.label1.text = posts[indexPath.row].title
cell.label2.text = posts[indexPath.row].message
}
return cell
}
Note: I found the code above to load the table online. This was my more simple code below that wasn't working properly.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:MyTableCell = self.tableView.dequeueReusableCell(withIdentifier: "Cell") as! MyTableCell
cell.label1.text = posts[indexPath.row].title
cell.label2.text = posts[indexPath.row].message
return cell
}
If nil is returned when calling tableView.dequeueReusableCell, you must instantiate the cell using the designated UITableViewCell initializer. The code you provided turns around and calls tableView.dequeueReusableCell again which will once again return nil.
To instantiate a new cell, use the following:
let cell = MyTableCell( style: .default, reuseIdentifier: "MyTableCell" )
I have an interesting problem. as i am new to Swift.
I have created on TableView and added CUSTOM CELL using Storyboard. Now i want add another CUSTOM CELL When click on first CUSTOM CELL UIButton.
Second Custom Cell is created using XIB. now when i register That second Cell in didload then i see blank tableview as Second custom cell is Blank.
i have used following Code:
for registering Second cell
self.tableView.registerNib(UINib(nibName: "customCell", bundle: nil), forCellReuseIdentifier: "customCell")
and cell for row at index
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! Cell
cell.nameLbl.text = "Hello hello Hello"
let Customcell = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! customCell
if self.Selected == "YES" {
if self.selectedValue == indexPath.row {
return Customcell
}
return cell
}
else{
return cell
}
}
Here Cell object is for Storyboard Cell and Customcell is for XIB Second custom cell.
please suggest me how to do that.
First ensure your ViewController is the UITableViewDelegate and UITableViewDataSource of the tableView, and that you have an outlet for the tableView
Next you need to register the custom cell in the viewDidLoad method:
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "customCell")
}
It is easiest to save an array of cells that have been selected if you are going to have more than 1 cell that you want to modify when pressed. This can be a variable within the ViewController:
var customCellIndexPaths: [IndexPath] = []
when a cell is selected you can simply add it to the array of custom cell IndexPaths (if it is not already a custom cell), and then reload that cell:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if customCellIndexPaths.contains(indexPath) == false {
customCellIndexPaths.append(indexPath)
tableView.reloadRows(at: [indexPath], with: .automatic)
}
}
In the cellForRowAt method we must check whether the cell has been selected and if so return the custom cell, else return a normal cell:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if customCellIndexPaths.contains(indexPath) {
return tableView.dequeueReusableCell(withIdentifier: "customCell")!
}
let cell = UITableViewCell(style: .default, reuseIdentifier: "normalCell")
cell.textLabel?.text = "Regular Cell"
return cell
}
There you have it. Now you should receive a smooth animation of a normal cell becoming a CustomCell when being selected.
I've been trying and researching for the whole day and still not able to find the right answer.
I have a button and I want to insert customized UITableViewCell in my static UITableView upon button click.
Here's what I have done. I created an xib file with my customized UITableViewCell and have assigned swift file to it.
In the viewdidload of my tableviewcontroller, I registered my UITableViewCell:
override public func viewDidLoad() {
super.viewDidLoad()
let nib = UINib(nibName: "TableViewCell", bundle: nil)
addContactTableView.registerNib(nib, forCellReuseIdentifier: "cell")
}
Here's my function for my button click:
#IBAction func buttonClick(sender: AnyObject) {
if let button = sender as? UIButton {
if let superview = button.superview {
if let cell = superview.superview as? UITableViewCell {
indexPath_g = self.tableView.indexPathForCell(cell)!
}
}
}
print(indexPath_g.row)
addContactTableView.beginUpdates()
addContactTableView.insertRowsAtIndexPaths([
NSIndexPath(forRow: indexPath_g.row, inSection: 1)
], withRowAnimation: .Automatic)
addContactTableView.endUpdates()
}
And my cellForRowAtIndexPath:
public override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath_g) as! TableViewCell
if(indexPath.section == 1) {
//Configure the cell...
cell.detail_field.placeholder = "Phone"
}
return cell
}
I tried to run this and I keep getting this error:
fatal error: unexpectedly found nil while unwrapping an Optional value
Insert the UITableviewCell in button action. When tapping on button, you can get the indexPath of the cell and insert anywhere in the UItableview.Just add two methods [tableView beginUpdate] and [tableView endUpdate] after inserting the cell using insertRowsAtIndexPaths method.
My first guess is your cellForRow method:
public override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath_g) as! TableViewCell
...
}
change indexPath_g to indexPath