Subclassing UITableViewCell on Swift - Error edit UITableView rows - uitableview

Using Swift: I've hooked up an NSFetchedResultsController to a UITableView, and created a subclass of UITableViewCells for the TableView cells.
The UITableViewCells format and display correctly. But the app crashes as soon as I try editing the rows (adding or deleting).
For testing, I've used the most basic subclass possible: An empty subclass, and it still crashes. The original UITableViewCell works normally.
Any ideas? Swift bug? The crash goes straight to the top of the stack. I get nothing in the console.
Works:
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell? {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
let task = fetchedResultController.objectAtIndexPath(indexPath) as Tasks
cell.textLabel.text = task.desc
return cell
}
Doesn't work:
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell? {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell2
let task = fetchedResultController.objectAtIndexPath(indexPath) as Tasks
cell.textLabel.text = task.desc
return cell
}
When subclass is:
import UIKit
class UITableViewCell2: UITableViewCell {
}

If you create table cells programmatically (not in InterfaceBuilder) you have to register the table cell class with
TableView.registerClass(..., forCellReuseIdentifier...)

Related

Two TableViews that have same prototype cell, constraints behave differently

I created two ViewControllers and two TableViews. Then i added prototype cell to one TableView, set it up according to my needs, copied it to the other TableView, changed its class and identifier and linked it up in ViewController that is datasource and delegate for each one.
The problem is, FEEDING one is behaving good, having constraints as expected, and the WALKING one is not, but i have no idea why since they have all same properties in each one's:
ViewControllers:
FEEDING
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = myFeedingTableView.dequeueReusableCellWithIdentifier("feedingcell", forIndexPath: indexPath) as! FeedingCell
cell.time.text = self.vremena[indexPath.row]
return cell
}
WALKING
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = myWalkingTableView.dequeueReusableCellWithIdentifier("walkingcell", forIndexPath: indexPath) as! WalkingCell
cell.time.text = self.vremena[indexPath.row]
return cell
}
CustomCell files
each one is connected to its class
FeedingCell is class of feeding prototype cell
WalkingCell is class of feeding prototype cell
Constraints
and the constraints are same, as you can see on the picture.
Here is the image providing different results and constraints:
image
Solved by changing rowHeight settings in TableView. Thanks #SilentLupin

Swift : UITableViewCell Overlapping in debug view Hierarchy

Cells Overlapping
I used custom class for UITableViewCell. It look fine
but the debug view Hierarchy cells is overlapping as image above.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(self.reuseIdentify, forIndexPath: indexPath) as! ContactViewCell
cell.lbContactName.text = self.dataSource[indexPath.row]
return cell
}
Please check the custom UITableviewCell Clip Subviews property.

Get TableViewController from TableViewCell

I have a custom class for a table cell that is connected to a switch within the table cell (with an action) and I want to be able to to communicate to the TableViewController that the action happened as well as the path of the cell. The way that initially came to mind was if I could use some function in UITableViewCell to get the TableViewController of the table the cell is part of, as my custom class is (rather obviously) a subclass of UITableViewCell. Please tell me if I'm missing something.
To get a reference to the containing view controller, I add a weak property on the cell subclass.
#property (nonatomic, weak) UITableViewController *viewController;
You can assign this value in cellForRowAtIndexPath:
For accessing each cell in TableView
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as YourTableViewCell
cell.mainTextLabel.text = self.venueService.mainCategoriesArray()[indexPath.row]
return cell
}
For getting selected cell in TableView
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let indexPath = tableView.indexPathForSelectedRow();
let currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell;
print(currentCell)
}
Reaching from TableViewCell to TableView
self.superview //// self is TableViewCell and its superview represents TableViewController
hope that helps
You should make the ViewController the target of the switch action.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("SwitchCell", forIndexPath: indexPath) as! SwitchTableViewCell
cell.onSwitch.addTarget(self, action: Selector("cellSwitchDidChange:"), forControlEvents: .ValueChanged)
cell.label.text = "This is a Switch"
return cell
}
func cellSwitchDidChange(sender: UISwitch) {
let origin = sender.convertPoint(CGPointZero, toView: tableView)
let indexPath = tableView.indexPathForRowAtPoint(origin)!
// do something
}

cannot invoke 'dequeReusable...' with an argument of type '(String)'

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel!.text = self.funlists[indexPath.row]
return cell
}
cannot invoke 'dequeReusable...' with an argument of type '(String)'
on line var cell:UITableViewCell =
self.tableView.dequeueReusableCellWithIdentifier("cell") as
UITableViewCell
(sorry i'm extremely new to Xcode and swift)
when i click my UITableView in my storyboard, it is a table view cell with style Subtitle and identifier cell. (I may be doing this completely wrong, not sure)
I think I find the problem, try to take the word self of the dequeueReusableCellWithIdentifier call, like this:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel!.text = self.funlists[indexPath.row]
return cell
}
The reason been that you want to use the tableView coming from the function and not the tableView that belongs to the class.
Let me know if this solve your problem
Please define your cell identifier in storyboard.

UITableView Cells Repeating First Cell

My TableView has been repeating the first cell and I had trouble figuring out the problem. I've searched around here and found a relevant thread (UITableView Cells All Repeat 1st Cell).
I figured out that the problem was that I was not specifying the section in my cellForRowAtIndexPath method. I understand that I have to implement indexPath.section into my code, but I can't figure out how/where.
Any help is greatly appreciated!
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath:indexPath) as CustomTableViewCell
// Configure the cell...
let entry = entries[indexPath.row]
cell.entryTextView!.text = entry.bodyText
cell.entryDayLabel!.text = entry.day
cell.entryDateLabel!.text = entry.date
return cell
}

Resources