UICollectionView inside UITableView - swift

So I'm trying to create a collection view inside a tableview following this:
https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/
All is going well until a problem occurs here.
override func tableView(tableView: UITableView,
willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath) {
guard let tableViewCell = cell as? TableViewCell else { return }
tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
}
I receive the error 'Cannot invoke 'setCollectionViewDataSourceDelegate' with and argument of list type '(ViewController, forRow: Int)''
What's the problem here?

Related

Textfield values deleted when scrolling tableview?

I have tableview cell with a textfield but when I add new row and scroll tableview up or down disappeared textfield values deleted. I make lots of research about that previous question about that in 2015 and its not answered correctly. How can I fix this issue? How can I use textfield delegate method if it is working for this? Here my code:
var i = 0
while i <= taskArrForRead.count {
let indexPath = IndexPath(row: i, section: 0)
let cell = self.tableView.cellForRow(at: indexPath) as? taslakDuzenlemeCell
if let item = cell?.taslakTextField.text! {
arrayOfNames.append(item)
}
i = i + 1
}
In this code I get all textfield values from tableview but if tableview scroll disappeared values turn with default values. How can I fix it? Thanks.
Here my tableview code:
extension taslakOlusturmaController: UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return taskArrForRead.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "taslakCell", for: indexPath) as! taslakDuzenlemeCell
cell.taslakTextField.text = taskArrForRead[indexPath.item]
cell.taslakTextField.delegate = self
return cell
}
func textFieldDidEndEditing(_ textField: UITextField) {
print(textField.text!)
self.arrayOfNames.append(textField.text!)
}
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "taslakCell", for: indexPath) as! taslakDuzenlemeCell
cell.taslakTextField.text = taskArrForRead[indexPath.item]
print(arrayOfNames[indexPath.item])
}
}
With textfield delegate method I can get deleted textfield values but I can't bring it again to textfield. User can't see value after scroll again also I can get deleted values with didEndDisplaying cell method too.
You could use the method PrepareForReuse to set the text back when the cell recreated again.

Typecasting with variable

I have an enum that will indicate the identifier and class to use for a tableViewCell.
enum Settings: String {
case Delete = "deleteCell"
var cellType: UITableViewCell.Type {
switch self {
case .Delete: return DeleteCell.self
}
}
}
In my tableView rowForIndexPath I am able to get the cell with the reuseIdendifyer, but it won't recognize the setting object to get the class. The error here is Use of undeclared type 'setting', specifically on the end of the line.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let setting = Settings.enabledSettings[indexPath.row]
return tableView.dequeueReusableCell(withIdentifier: setting.rawValue, for: indexPath) as! setting.cellType
}
It's not bad now with just the one object, but this will be growing soon and it's mostly the same thing, just changing the class name. Is it possible to pull the class type from another source like this so I can keep the rowForIndexPath function short and sweet, or do I need to do something like this?
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let setting = Settings.enabledSettings[indexPath.row]
var cell = tableView.dequeueReusableCell(withIdentifier: setting.rawValue, for: indexPath)
if setting == .Delete { cell = cell as! DeleteCell }
return cell
}

How to add table view cell with a button inside of another table view cell?

I am trying to make a to-do list app and I am having trouble trying to add subtasks to my main to-do tasks. I have a button inside each table view cell, when this button is pressed I want it to add another table view cell that I can type and save a "subtask" on.
//set up tableView row amount
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return taskData.count
}
//set up what is in the tableView row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//This is for a main task
let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath) as! CustomTableViewCell
cell.label.text = taskData[indexPath.row]
return cell
}
Use delegates to pass data from cells to your ViewController, and reload the tableView.
CustomTableViewCell:
protocol CustomTableViewCellDelegate: class {
func customTableViewCellButtonClicked(_ cell: CustomTableViewCell)
}
class CustomTableViewCell: UITableViewCell {
weak var delegate: CustomTableViewCellDelegate?
func buttonClicked() {
self.delegate?.customTableViewCellButtonClicked(self)
}
}
ViewController:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath) as! CustomTableViewCell
cell.label.text = taskData[indexPath.row]
cell.delegate = self
return cell
}
func customTableViewCellButtonClicked(_ cell: CustomTableViewCell) {
// add the task you need from that cell to your tasks list.
taskData.append(....)
//reload your tableView
self.tableView.reloadData()
}

Create object that conform to protocol in Swift

I want to create cell that i know, is conformable to specific protocol.
However, when i try to do :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell<ModelBinding> = tableView.dequeueReusableCell(withIdentifier: "VacanciesCell", for: indexPath as IndexPath)
return cell
}
I got an error. How to fix it?
You have to subclass cell , which confirms the protocol that you want to use. Here i have created a sample protocol and CustomCell which confirming the protocol i have created.
1. Sample protocol
protocol MyProtocol {
func protocolMethod()
}
2. Custom subClassed cell
class CustomCell:UITableViewCell,MyProtocol {
//Implementation of Protol method
func protocolMethod() {
}
}
3. Use of that cell on tableview
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:CustomCell = tableView.dequeueReusableCell(withIdentifier: "VacanciesCell", for: indexPath as IndexPath) as! CustomCell
return cell
}

Why does my app crash on "indexPath.row" load?

I may not be providing enough information to properly ask this question, but I am trying to discern why I am getting a crash when my code reaches the loading of indexPath.row.
With much of the code truncated, here is my TableViewController.swift:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return minions.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return minionCellAtIndexPath(indexPath)
}
func minionCellAtIndexPath(indexPath:NSIndexPath) -> MinionCell {
let minion = minions[indexPath.row]
//Set the cell to be the custom, reusable, MinionCell
let cell = tableView.dequeueReusableCellWithIdentifier(minionCellIdentifier, forIndexPath: indexPath) as! MinionCell
if let name = minion.name {
cell.nameLabel?.text = minion.name
} else {
cell.nameLabel?.text = "No data available."
}
return cell
}
From what I can discern, the let minion = minions[indexPath.row] is throwing an error, with the error occurring on __Thread 1__ and the syntax showing:
function signature specialization Arg[0] = Owned To Guaranteed
The console output seems to show nothing indicating what the error may be, though I'm questioning if this has to do with my transition to Swift 2 from Swift 1.2.
problem should be with dequeueReusableCellWithIdentifier,Ensure it is set properly in storyboard.Best place to give identifier name for your cell is, your minioncell class.
I ended up resolving this issue by altering;
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return minionCellAtIndexPath(indexPath)
}
to
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
By altering the return function and adding all of my table cell configuration to this function, I found complete success in properly populating the table.