tableview

UITableview

properties:

var tableviewTitles = ["item 1","item 2", "item 3","item 4","item 5","item 6"]

in viewdidload()

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

obliged data en source methods

//MARK: tableview
  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return tableviewTitles.count
  }
 
  func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
  }
  let cellIdentifier = "cell"
  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as UITableViewCell
    cell.textLabel?.text = tableviewTitles[indexPath.row]
   
    return cell
   
  }

with a lot of (large) images:

extension ViewController{
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return images.count
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
        cell.textLabel?.text = images[indexPath.row]
        if cache.objectForKey("\(indexPath.row)") != nil
        {
            cell.imageView?.image = cache.objectForKey("\(indexPath.row)") as! UIImage
        }
        else{
           
        }
        return cell
    }
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if cache.objectForKey("\(indexPath.row)") != nil
        {
            cell.imageView?.image = cache.objectForKey("\(indexPath.row)") as! UIImage
        }
        else{
            cell.imageView?.image = UIImage(named: "placeholder.png")
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
                if let img = UIImage(named: "\(self.images[indexPath.row]).png")
                {
                    cell.imageView?.image = img
                    self.cache.setObject(img, forKey: "\(indexPath.row)")
                }
               
            }
        }
       
       
      
    }
}

Tags: