User1238 User1238 - 5 months ago 11
iOS Question

Customize TableViewCell with swift

I am trying to add subtitle to cells in a table view. I am trying this code but the subtitle is not showing.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell=tableView.dequeueReusableCellWithIdentifier("topCell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text=listPerson[indexPath.row].name
cell.detailTextLabel?.text="test"
var personImage=UIImage(named: listPerson[indexPath.row].country)
cell.imageView?.image=personImage

return cell
}


I also want to know if it's possible to add another image in the right of the cell.

Answer

To display the text in detailTextLabel you have to set the style of the cell in the Attributes Inspector of Interface Builder (⌥⌘4)to Subtitle.

To add a custom image on the right edge you can create the UIImageView instance programmatically and add it as subview to the accessory view.

However it might be more suitable to use a custom cell class providing an outlet.