How to set data from ViewController to UITableView custom Cell - ios

I have been trying to pass data from Viewcontroller to UITableView custom cell using custom delegate.There are answers for passing data from Custom cell to ViewController but none for the viceversa .Can anyone suggest me with some idea or sample code.

I am not sure if this is what you mean, but you can set the data in function tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
For example:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: ViewController.cellIdentifier, for: indexPath) as! CustomCell
cell.data = data
return cell
}

Related

How to make selection style in UITableViewCell

I created Xib file for my uitableviewcell in the attributed inspector i choose SelectionStyle default, but it didn't help me. So and i added accessory type .detailDisclosureButton.
So when i tapped on a cell , My cell doesn't highlighted gray color, only disclosurebutton does
Maybe you can try this one,
extension CWScoreBoardViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
}
or you can try this,
extension CWScoreBoardViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CWUsersTableViewCell", for: indexPath) as! CWUsersTableViewCell
cell.selectionStyle = .none
return cell
}
}

Ios show/hide views inside a tableview cell

I have a tableview with a custom cell. Below image shows the view hierarchy of my tableview cell.
When adding rows to the tableview I'm hiding the 'check Image' imageview using below code.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "accountCell", for: indexPath) as! AccountsTableViewCell
cell.checkImage.isHidden = true
return cell
}
and when a row is clicked I'm showing the imageview again. Below is the code
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "accountCell", for: indexPath) as! AccountsTableViewCell
cell.checkImage.isHidden = false
}
But the problem is when I click a row nothing happens. system execute the cell.checkImage.isHidden = false code line, but the imageview doesn't appear. It is still hidden. Could someone tell me what I'm doing wrong here?
You can't track cell checked status in your cell itself; the cell object is just a view onto your data and cells will be reused when the table view scrolls.
I suggest using a Set<IndexPath>. In your cellForRowAt you can check the contents of the set in order to determine whether the checkmark should be hidden:
var checked = Set<IndexPath>()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "accountCell", for: indexPath) as! AccountsTableViewCell
cell.checkImage.isHidden = self.checked.contains(indexPath)
return cell
}
in your didSelectRowAt you simply toggle the set membership and reload the row
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if self.checked.contains(indexPath) {
self.checked.remove(indexPath)
} else {
self.checked.insert(indexPath)
}
tableView.reloadRows(at:[indexPath], with:.fade)
}
You have to manage your array (datasource)! when you click your row, update your array for that index and reload your table view.
Example :
Your array should have some flag like isChecked toggle it's value from true to false or vice-versa.
In your cellForRowAtIndexPath check this flag and show or hide your image according to that flag!
first be sure your
tableview.delegate = self
second thing in did select row at index use that code
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! AccountsTableViewCell
cell.checkImage.isHidden = false
}

TableViewCell Unresolved Identifier when creating cell

in my Xcode Project, I have the main storyboard set up with a table view and a prototype cell with the identifier set to "cell". The table view has a data source outlet and a delegate outlet.
Yet, my build crashes every time because of an "unresolved identifier" when I create the table view in line 11 (see code below).
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{
let array = createArray()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return(validDeck.count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
cell = UITableViewCell(style: UITableViewStyle.default, reuseIdentifier: "cell") |"ERROR: Unknown Identifier 'cell' "
cell.textlabel?.text = ""
return cell
}
Does anybody know this error and is willing to help?
Use below code for swift 3.*
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return validDeck.count // assuming count must be greater then 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
cell.textlabel?.text = "Cell tittle"
return cell
}
You never initialize UITableViewCell instances directly.
In viewDidLoad:
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
and then:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for indexPath: indexPath)
cell.textlabel?.text = ""
return(cell)
}

Swift 3 error with tableView

An error keeps occurring even changing the new swift3.0 syntax like removing the NS prefixes and other. The error says
cannot call value of non-function type UITableView
I'm glad if I could have a hint for solving this problem.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {
let cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath)
return cell
}
You need to return cell because cellforRowAtIndexPath is non void function :
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {
let cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath)
return cell
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cartcel", for: indexPath) as! cartTableViewCell // My custom tableview cell class
cell.productName.text = Addtocartdata[indexPath.row].cartproName
cell.productQty.text = Addtocartdata[indexPath.row].cartproPrice
cell.productAmount.text = Addtocartdata[indexPath.row].cartproPrice
return cell;
}

Segue is not working from static UITableView

I have a TableViewController with static cells
I tried both creating segue from TableViewController and TableView cell (Not at the same time)
However, in both scenario, didSelectRowAtIndexPath was not fired
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("select")
}
I also have embedded collectionviewcontroller
class EventDetail: UITableViewController, UITextFieldDelegate, UICollectionViewDataSource, UICollectionViewDelegate {
What may be causing this?
Check your tableview selection must be single selection, if it is "No selection" then didSelectedRowAtIndex would not get called.
You can download sample code and observe it.
Sample download
Also check
Ideally your cellForRowAtIndex should be like this
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell : CustomCell! = tableView.dequeueReusableCellWithIdentifier("ID_CustomCell", forIndexPath: indexPath) as! CustomCell
cell.selectionStyle = UITableViewCellSelectionStyle.None
cell.lblData.text = "CustomCell....."
return cell
}
Swift 4 code
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : CustomCell! = tableView.dequeueReusableCell(withIdentifier: "ID_CustomCell", for: indexPath) as! CustomCell
cell.selectionStyle = .none
cell.lblData.text = "CustomCell....."
return cell
}
call performseguewithidentifier from didselectRow if you have created segue from tableviewcontroller. like,
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("select")
self.performSegueWithIdentifier("your segue identifier", sender: self)
}
hope this will help :)

Resources