CHRIS
CHRIS

Reputation: 31

swift 3 UITableView

I'm a newbie.
But I do not understand why the UITableView was not all elements loads (10 element in array).
With Xcode7 works but with Xcode8 (Swift 3) no.
I rewrote the code but to no avail: one element is being loaded ...

This is the code inside the UIViewController (with extension UITableViewDelegate, UITableViewDataSource)

CODE:

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

// cell height
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true  
}

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

    let cell = mytableView.dequeueReusableCell(withIdentifier: "CellComment", for: indexPath ) as! commentCell


    // connect objects with our information from arrays
    cell.usernameBtn.setTitle(usernameArray[indexPath.row], for: UIControlState.normal)
    cell.usernameBtn.sizeToFit()
    cell.commentLbl.text = commentArray[indexPath.row]
    return cell
}

I think the problem is in the cellForRowAt....
I also have the following error:

BFTask caught an exception in the continuation block. This behaviour is discouraged and will be removed in a future release. Caught Exception: Invalid index path for use with UITableView. Index paths passed to table view must contain exactly two indices specifying the section and row. Please use the category on NSIndexPath in UITableView.h if possible.

I do not understand. Does anyone have an idea? Thank you

Upvotes: 3

Views: 9775

Answers (3)

Archit
Archit

Reputation: 31

There can some problems in your code. Try resolving/checking the following possible mistakes :

  1. According to your error the problem might be in the subscripting of your arrays. Try this:

    usernameArray[indexPath.section][indexPath.row]

And also use the delegate:

func numberOfSections(in tableView: UITableView) -> Int {

    return 1
}
  1. remove estimatedHeightForRowAt and add the following code in viewDidLoad() function

    tableView?.estimatedRowHeight = 135 //This value should be nearest to your cell height. tableView.rowHeight = UITableViewAutomaticDimension

  2. you might not have set the delegates for tableViewDataSource and tableViewDelegate. Set the delegate from the storyboard or in viewDidLoad() method.

Upvotes: 0

Frankenstein
Frankenstein

Reputation: 16341

Changing mytableView to tableView inside cellForRowAt indexPath delegate method should solve the issue.

let cell = tableView.dequeueReusableCell(withIdentifier: "CellComment", for: indexPath ) as! commentCell

Upvotes: 0

Mehul Solanki
Mehul Solanki

Reputation: 463

You need to use following lines inside CellForRowAt IndexPath:

let cellIdentifier = "CellComment"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as UITableViewCell?

Upvotes: 1

Related Questions