Thursday, February 21, 2019

Custom Swift tableView

We want our list have an icon at left, drag icon in to project, add this line in function tableView.
cell.imageView?.image = UIImage(named: "image")
To change arrow mark at right.
cell.accessoryType = .Checkmark
If want many images at left, declare images with their names.
let image = UIImage(named: "icon")
     let image2 = UIImage(named: "icon2")
     let image3 = UIImage(named: "icon3")
     let image4 = UIImage(named: "icon4")
     let image5 = UIImage(named: "icon5")
     let image6 = UIImage(named: "icon6")
     let image7 = UIImage(named: "icon7")
     let image8 = UIImage(named: "icon8")
    let image9 = UIImage(named: "icon9")
  Create array to store them.
var icon = [image,image2,image3,image4,image5,image6,image7,image8,image9]  
Use this line to set images
cell.imageView?.image = icon[indexPath.row]

If your icons not the same dimension, it will look like this.
Let replace the line cell.imageView?.image = icon[indexPath.row]
By these lines.
let im = icon[indexPath.row]
UIGraphicsBeginImageContextWithOptions(CGSizeMake(40,40), true, 0.0)
      im!.drawInRect(CGRectMake(0,0,40,40))
let im2 = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
        cell.imageView?.image = im2
Images will look like this.
If you want different color strings for each row, add this.
if indexPath.row == 0 {
             cell.textLabel?.textColor=UIColor.blueColor()
        }
            else if(indexPath.row == 1){
             cell.textLabel?.textColor=UIColor.blackColor()
            }
        else if(indexPath.row == 2){
            cell.textLabel?.textColor=UIColor.brownColor()
        }
        else if(indexPath.row == 3){
            cell.textLabel?.textColor=UIColor.redColor()
        }
        else if(indexPath.row == 4){
            cell.textLabel?.textColor=UIColor.grayColor()
        }
        else if(indexPath.row == 5){
            cell.textLabel?.textColor=UIColor.greenColor()
        }
        else if(indexPath.row == 6){
            cell.textLabel?.textColor=UIColor.orangeColor()
        }
        else if(indexPath.row == 7){
            cell.textLabel?.textColor=UIColor.cyanColor()
        }
        else {
            cell.textLabel?.textColor=UIColor.magentaColor()
        }


No comments:

Post a Comment