1
 booktable.frame = CGRect(x: 0, y: booktopview.bounds.height, width: screenWidth, height: screenHeight-booktopview.bounds.height-tabbarView.bounds.height)

            booktable.register(UITableViewCell.self, forCellReuseIdentifier: "mycell")
            booktable.dataSource = self
            booktable.delegate = self
            booktable.separatorColor = UIColor.lightGray
            booktable.backgroundColor = UIColor.clear
            booktable.separatorStyle = .singleLine

            bookview.addSubview(booktable)



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

if(tableView == booktable)
    {
     let cell1 = booktable.dequeueReusableCell(withIdentifier: "mycell")

        for object in (cell1?.contentView.subviews)!
        {
            object.removeFromSuperview();
        }

        let img :UIImageView = UIImageView()
        let lbl : UILabel = UILabel()


        img.frame = CGRect(x: 15, y: 15, width: 80, height: 130)
        img.image = imgarray[indexPath.row]
        img.layer.borderWidth = 1.0
        img.layer.borderColor = UIColor.lightGray.cgColor
        cell1?.contentView.addSubview(img)

        imgheight = img.bounds.height

        lbl.frame = CGRect(x: img.bounds.width + 40, y: (imgheight+40-80)/2, width: booktable.bounds.width-img.bounds.width + 40 - 100, height: 80)
        lbl.text = imgname[indexPath.row]
        lbl.numberOfLines = 0

        lbl.textAlignment = .left
        lbl.font = UIFont(name: "Arial", size: 23)
        lbl.textColor = UIColor.black
         cell1?.selectionStyle = .none



        cell1?.contentView.addSubview(lbl)

        return cell1!
    }

The code shown above is for book table, which sometimes scrolls like normal and sometimes not scrolling at all. I am doing all the code programatically. I have tested this on both simulators and devices but still the problem exists. Any help is appreciated...

aBilal17
  • 2,392
  • 1
  • 15
  • 23
vishnu
  • 193
  • 9

1 Answers1

2

Create Custom UITableViewCell, let's say it is ListTableCell

class ListTableCell: UITableViewCell {

    @IBOutlet weak var lblTemp: UILabel!
    @IBOutlet weak var imgTemp: UIImage!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}

I've created UITableViewCell with xib like this and bind IBOutlets

enter image description here

Let's say we have struct Model and array like this

struct Model {
    let image : UIImage
    let name: String
}

for i in 0...10 {
    let model = Model(image: #imageLiteral(resourceName: "Cat03"), name: "Temp \(i)")
    array.append(model)
}

Now on ViewController viewDidLoad() method,

tableView.register(UINib(nibName: "ListTableCell", bundle: nil), forCellReuseIdentifier: "ListTableCell")

Implement UITableViewDataSource methods like this,

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ListTableCell") as! ListTableCell
    let model = array[indexPath.row]
    cell.lblTemp.text = model.name
    cell.imgTemp.image = model.image        
    return cell
}

FYI

For different tableviews, you can create different custom cell the same way and cellForRowAt indexPath and numberOfRowsInSection method will change appropriately.

Let me know in case of any queries.

UPDATE

Follow this and this to create CustomTableCell programmatically

PPL
  • 5,800
  • 1
  • 8
  • 27
  • is there a way to not use xib's or storyboard? – vishnu May 30 '18 at 12:09
  • yup you can create your customcell programmatically also – PPL May 30 '18 at 12:11
  • i was trying to do that way and then the contends of tableview ovelaps, so i used the code for object in (cell1?.contentView.subviews)! { object.removeFromSuperview(); } it solved the issue in all 3 tableviews but scrolling issue popped on booktable only – vishnu May 30 '18 at 12:12
  • see my updated answer, there are links to create cell programmatically – PPL May 30 '18 at 12:14
  • You should extend UITableViewCell and on init method, you can addsubview to cell's contentview, no need to remove all subviews then because it will reuse the customcell itself – PPL May 30 '18 at 12:15