1

I have a UITableView which uses self sizing cells. Now the problem is the cell height is getting changed according to the content size but it is not scrollable. How can I make it Scrollable in Swift.items is a String array of 5 elements. This is my code. As far as I have read the documentation tableviews should be automatically scrollable. But I can only see the first two items and half of the third item. Here is the source code of Project Dropbox Link

class ViewController: UIViewController,UITableViewDelegate {
let basicCellIdentifier = "BasicCell"
var items = ["admksalmdlsamdlsamdlkasmdlasmdlsamdlksamdlkasmdlkasmdlksamdklasmdlkasmdlkasmdlkasmdcjkndscnksndcdkcnksdjcnksdnckjsdncsdnjkanckjanckjsnckjasncjkasnckjasncjkasnckdslnjcvlkjdncjsnjkasncjkasncjkasnckjasncjkasnckjsanckjsanckjasncjksancjksancjksancjkasncjasncjkasncklas;NKCNDJKVALNVJKSDANCKSCNJSNCJKSDCNKJSDNCKJANCDKNLSACNJDKCNJKNCAKJNACJKNKJANCKJSNC","badmksalmdlsamdlsamdlkasmdlasmdlsamdlksamdlkasmdlkasmdlksamdklasmdlkasmdlkasmdlkasmdcjkndscnksndcdkcnksdjcnksdnckjsdncsdnjkanckjanckjsnckjasncjkasnckjasncjkasnckdslnjcvlkjdncjsnjkasncjkasncjkasnckjasncjkasnckjsanckjsanckjasncjksancjksancjksancjkasncjasncjkasncklas;NKCNDJKVALNVJKSDANCKSCNJSNCJKSDCNKJSDNCKJANCDKNLSACNJDKCNJKNCAKJNACJKNKJANCKJSNC", "cadmksalmdlsamdlsamdlkasmdlasmdlsamdlksamdlkasmdlkasmdlksamdklasmdlkasmdlkasmdlkasmdcjkndscnksndcdkcnksdjcnksdnckjsdncsdnjkanckjanckjsnckjasncjkasnckjasncjkasnckdslnjcvlkjdncjsnjkasncjkasncjkasnckjasncjkasnckjsanckjsanckjasncjksancjksancjksancjkasncjasncjkasncklas;NKCNDJKVALNVJKSDANCKSCNJSNCJKSDCNKJSDNCKJANCDKNLSACNJDKCNJKNCAKJNACJKNKJANCKJSNCSDNSAJKDNAKLSMDLAKSDMKLASMDLKASMDLKASMDLKASMDLKASMDLKASMDLKASMDLKASMDLKASMDLKASMLAKSMDLASMDLAKSMDLKASMDLKASMDLKASMLKADSMLKASMDLKASMDLKMadmksalmdlsamdlsamdlkasmdlasmdlsamdlksamdlkasmdlkasmdlksamdklasmdlkasmdlkasmdlkasmdcjkndscnksndcdkcnksdjcnksdnckjsdncsdnjkanckjanckjsnckjasncjkasnckjasncjkasnckdslnjcvlkjdncjsnjkasncjkasncjkasnckjasncjkasnckjsanckjsanckjasncjksancjksancjksancjkasncjasncjkasncklas;NKCNDJKVALNVJKSDANCKSCNJSNCJKSDCNKJSDNCKJANCDKNLSACNJDKCNJKNCAKJNACJKNKJANCKJSNCishan","cool1"]
@IBOutlet weak var tableView: UITableView!
func configureTableView() {
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 160.0
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier(basicCellIdentifier) as BasicCell
    cell.titleLabel.text = items[indexPath.row]
   return cell
}


 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return items.count
}
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}
8
  • you have single cell in tableview? Apr 9, 2015 at 5:58
  • items is an array of 5 elements but all elements have very large number of characters.
    – Adam Young
    Apr 9, 2015 at 6:00
  • technically tableview inherits from uiscrollview,so in your case tableview should be scrollable by default,what is the problem you are getting? Apr 9, 2015 at 6:02
  • Do all 5 items are appearing in 5 different rows of table view? Apr 9, 2015 at 6:03
  • Pardon me if I understand your question wrong, add some more string values to items and let us know whether it is scrollable or not Apr 9, 2015 at 6:10

1 Answer 1

1

I have just run your project and I think I found the problem. You are not calling the configureTableView method anywhere.

I made the following change:

override func viewDidLoad() {
        super.viewDidLoad()
        self.configureTableView()
        tableView.reloadData()
        // Do any additional setup after loading the view, typically from a nib.
    }

The result i get is:

enter image description here

Hope this was the result you were trying to achieve. Let me know if you need more help.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.