1

I'm wondering if we can make a dynamic function to access the cell template (xib template) for dynamic like this:

func create_image(TemplateName:??? = TemplateCell) {
    var cell = self.tableView.cellForRow(at: currindexpath as IndexPath) as! TemplateName
}

I want to pass the "TemplateName" with the name of class of UITableViewCell:

class template1: UITableViewCell {
}

so on that parameter I can pass any template cell that I made. Is that possible? Sorry if hard to explain.

2
  • why do you need to create a separate function ? You can use closures as delegates within cell class. Commented Nov 23, 2017 at 4:38
  • hmm sorry if i am not right to answer this..because possibility each row cell have different template layout...so i need to create this dynamic Commented Nov 23, 2017 at 7:27

3 Answers 3

1

In a word: no. Swift is not dynamic in the way that you would like. The only thing that can go after as! is an actual literal type name — not some sort of reference to a type that you would pass in a variable or parameter.

You could, as an alternative, dequeue the cell and then ask what type the cell is, thus casting down safely and explicitly:

let cell = // ... dequeue the cell as a UITableViewCell
if let cell = cell as? MyTableViewCell {
    // here, cell is a MyTableViewCell now
}

Using a switch statement instead of if, you could do that for all your different cell subclasses.

Sign up to request clarification or add additional context in comments.

1 Comment

seems to be right...still need to be manually to compare between it...so there's no way to do that..thank you!
1
I have done this in one my sample project: Here is the sample code-

    func setCardLayoutAccordingToId(cell: UITableViewCell,layoutId:Int) -> UITableViewCell {

       let cgRect: CGRect = cell.contentView.frame
        var layoutView: Any = DefaultCardView(frame: cgRect)

        switch layoutId {
        case 1:
            layoutView = DefaultCardView(frame:cgRect)

        case 2:
            layoutView = Template_1(frame:cgRect)

        case 3:
            layoutView = Template_2(frame:cgRect)

        case 4:
            layoutView = Template_3(frame:cgRect)

         default:
            layoutView = DefaultCardView(frame:cgRect)
            (layoutView as! DefaultCardView).setData(cardData: data)
        }

       for view in (cell.contentView.subviews)!{
            view.removeFromSuperview()
        }
        cell.contentView.viewWithTag(1)?.addSubview(layoutView as!  UIView)

(layoutView as! UIView).translatesAutoresizingMaskIntoConstraints = false

        let attributes: [NSLayoutAttribute] = [.top, .bottom, .right, .left]
        NSLayoutConstraint.activate(attributes.map {
            NSLayoutConstraint(item: (layoutView as! UIView), attribute: $0, relatedBy: .equal, toItem: (layoutView as! UIView).superview, attribute: $0, multiplier: 1, constant: 0)})
return cell }

P.S: layout_Id can be decided as per your need.

1 Comment

hi yes..so we still need to manually to catch the layoutID, i wish can just directly using the class name to pass the parameter..but this is great example..thank you!
0

You can pass the TemplateCell in a function indeed. For example:

func yourMethodForCell(_ cell: TemplateCell){

    cell.backgroundColor = .red
}

Then call as follows:

yourMethodForCell(cell)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.