I'm trying to create a tableView that has (for now) only a button in each cell.
I hard coded the number of rows to be 100:
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 100
}
And this is my cellForRowAt function that creates the button and is suppose to properly place it in each cell:
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
let row = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let btn = UIButton(type:(UIButtonType.custom)) as UIButton
btn.backgroundColor = UIColor.black
btn.setTitle("(read more)", for: UIControlState.normal)
btn.titleLabel!.font = UIFont(name: "Helvetica", size: 10)
btn.frame = CGRect(x:300, y:row.bounds.height, width:70, height:20)
btn.addTarget(self, action: Selector(("buttonPressed:")), for: UIControlEvents.touchUpInside)
btn.tag = indexPath.row
cell.contentView.addSubview(btn)
return cell
}
So this places the buttons in each cell, but when I click on the cell, the associated button disappears or when i scroll down, most of the first few buttons are gone when I scroll back up. What am I doing wrong here? Any help would be greatly appreciated!
Thanks in advance.