When I try to apply "Font" to one cell of the table, it is automatically applied to other cells when I view the scrolling table
I have my own table view.
I need to set the Bold Italic font (Helvetica-BoldOblique) to a cell only. But when it scrolls through the table view, it also applies to other cells one by one. How to solve this?
func applyFontToTableviewCell() {
var couIn = NSIndexPath(forRow: 2, inSection: 0)
var couCell = colorTableView.cellForRowAtIndexPath(couIn)
couCell?.textLabel?.font = UIFont(name: "Helvetica-BoldOblique", size: 18.0)
}
I tried this same code in cellForRowAtIndexPath as well. But the same problem came up.
Thanks in Advance.
+3
source to share
3 answers
You can apply bold to an alternate cell such as gaps.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let couCell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as UITableViewCell
let row = indexPath.row
if row == 2 || row == 4 || row == 6 {
couCell?.textLabel?.font = UIFont(name: "Helvetica-BoldOblique", size: 18.0)
}
else{
couCell?.textLabel?.font = UIFont(name: "Helvetica", size: 18.0)
}
return cell
}
Hope it helps you.
+2
source to share
You should check if the conditions are:
if (NSIndexPath(forRow: 2, inSection: 0)){
couCell?.textLabel?.font = UIFont(name: "Helvetica-BoldOblique", size: 18.0)
}
else{
//set your default font
couCell?.textLabel?.font = UIFont(name: "Helvetica", size: 18.0)
}
if you want to check odd and even then
if yourindexpath % 2 == 0 {
}
so maybe it would be like
if (NSIndexPath(forRow: indexPath.row % 2, inSection: 0)){
couCell?.textLabel?.font = UIFont(name: "Helvetica-BoldOblique", size: 18.0)
}
else{
//set your default font
couCell?.textLabel?.font = UIFont(name: "Helvetica", size: 18.0)
}
+2
source to share
I have the same problem and it is fixed by applying the font in the method willDisplayCell
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let row = indexPath.row
if row == 2 || row == 4 || row == 6 {
cell.textLabel?.font = UIFont(name: "Helvetica-BoldOblique", size: 18.0)
}
else{
cell.textLabel?.font = UIFont(name: "Helvetica", size: 18.0)
}
}
0
source to share