Dmitriy Vinnichuk Dmitriy Vinnichuk - 1 month ago 9
iOS Question

TableView constructible content | Swift

I'm working on an app, where admin-side is based on creation articles. Have 2 buttons - add image and text view. The order of adding is absolutely random, i.e. image-image-image-text / text-image-image etc.

I don't understand how to make it and add to a cell.
Adding an image:

prototype

Answer

To solve this Problem you have to make Custom UITableViewCell to add Image and textview.

After that you have prepare one array to add button which is clicked by user.(if button image press then i will add value “Image” and if textView button press then i will add value “TextView”)

And finally on the basis of that array you can return UITableViewCell.

This is the button click event

@IBAction func btnImageCliked(sender :UIButton)
{
  self.testArray.addObject("Image")
  self.tblCustom?.reloadData()
}

@IBAction func btnTextViewCliked(sender :UIButton)
{
    self.testArray.addObject("TextView")
    self.tblCustom?.reloadData()
}

Create Custom UITableViewCell object like this way,

  var imgCell : imageCell = imageCell()
  var txtCell : textViewCell = textViewCell()

And you can fill UITableView In this Way,

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.testArray.count
}

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

    let nib: NSArray = NSBundle.mainBundle().loadNibNamed("CustomView", owner: self, options: nil)
    if(testArray.objectAtIndex(indexPath.row) as! String == "Image")
    {
        imgCell = (nib.objectAtIndex(0) as? imageCell)!
        return imgCell
    }
    else
    {
        txtCell = (nib.objectAtIndex(1) as? textViewCell)!
         return txtCell
    }
    return UITableViewCell()
}