Swift: Create static custom cell without dequeueReusableCellWithIdentifier

1.1k views Asked by At

Good morning all, i need to instantiate my custom cell named "CustomCell" in my custom tableViewController without dequeueReusableCellWithIdentifier because this function, due to some interaction that i think that is not necessary to describe for now, replace incorrectly my cells while i'm scrolling (IE the last cell swap with the first and so on).

So my code is something like:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
-> CustomCell {

        // NEW VERSION HERE ---------
        var cell = CustomCell()
        cell.selectionStyle = UITableViewCellSelectionStyle.None
        cell.userInteractionEnabled = true
        cell.clipsToBounds = false

    // OLD VERSION HERE ---------
    //var cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as! CustomCell

    // Configure my cells....
    return cell
}

So is this version (with no dequeueReusableCellWithIdentifier) i've no more problems with cell's positioning but i can't click/tap or simply "use" my custom item into cell.

For example i've a DatePicker like this:

var dynamicPicker = UIDatePicker(frame: CGRectMake(0, 0, self.frame.width, 192.0))
        dynamicPicker.date = date_
        dynamicPicker.addTarget(self, action: "pickerChanged:", forControlEvents: UIControlEvents.ValueChanged)
        view.addSubView(dynamicPicker)

and i can't even change the date, i can't tap or generally i can't interact with it. It seems like "userInteractionEnabled is false" but it isn't.

Thank you all in advance, Best regards Marco.

1

There are 1 answers

0
jnoor On

There is a reason that every UITableView example uses dequeueReusableCellWithIdentifier. It's how a table view was meant to be used.

It seems the issue you are actually trying to solve is "the last cell swap with the first and so on." Perhaps that should be your question instead.

In your cell configuration (after dequeue...), reset all of your cells data. Can you explain further what is the problem when using dequeue?