2017-10-04 114 views
0

我想在UITableViewCell下添加一張圖片和一些文本。下面是我想達到的效果:以編程方式在UITableViewCell下添加UITextView和UIImageView Swift 4

Here is what I want to achieve

但我只能用純代碼實現的UITableViewCell。任何人都知道如何在UITableViewCell下面或上面添加圖片或文本?任何建議,將不勝感激。這是我目前的結果是:

My current result

這裏是我的代碼:

import UIKit 

class AboutViewController : UITableViewController { 
    private let about = ["About us"] 
    private let termsOfService = ["Terms of service"] 
    private let privacyPolicies = ["Privacy policies"] 
    private let openSourceLicense = ["Open Source Libraries"] 
    private let sections = ["about", "termsOfService", "privacyPolicies", "openSourceLicense"] 

    let myTableView: UITableView = { 
     let mt = UITableView() 
     return mt 
    }() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     //   register cell name 
     myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell") 
     // set DataSource 
     myTableView.dataSource = self 
     // set Delegate 
     myTableView.delegate = self 
    } 

    override func numberOfSections(in tableView: UITableView) -> Int { 
     return sections.count 
    } 

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
     return tableView.rowHeight 
    } 

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     if indexPath.section == 0 { 
      print("Value: \(about[indexPath.row])") 
     } else if indexPath.section == 1 { 
      print("Value: \(termsOfService[indexPath.row])") 
     } else if indexPath.section == 2 { 
      print("Value: \(privacyPolicies[indexPath.row])") 
     } else if indexPath.section == 3 { 
      print("Value: \(openSourceLicense[indexPath.row])") 
     } 
    } 

    // return the number of cells each section. 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if section == 0 { 
      return about.count 
     } else if section == 1 { 
      return termsOfService.count 
     } else if section == 2 { 
      return privacyPolicies.count 
     } else if section == 3 { 
      return openSourceLicense.count 
     } else { 
      return 0 
     } 
    } 

    // return cells 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "MyCell") 

     if indexPath.section == 0 { 
      cell.accessoryType = .disclosureIndicator 
      cell.textLabel?.textAlignment = .left 
      cell.textLabel?.text = "\(about[indexPath.row])" 
     } else if indexPath.section == 1 { 
      cell.accessoryType = .disclosureIndicator 
      cell.textLabel?.textAlignment = .left 
      cell.textLabel?.text = "\(termsOfService[indexPath.row])" 
     } else if indexPath.section == 2 { 
      cell.accessoryType = .disclosureIndicator 
      cell.textLabel?.textAlignment = .left 
      cell.textLabel?.text = "\(privacyPolicies[indexPath.row])" 
     } else if indexPath.section == 3 { 
      cell.accessoryType = .disclosureIndicator 
      cell.textLabel?.textAlignment = .left 
      cell.textLabel?.text = "\(openSourceLicense[indexPath.row])" 
     } 

     return cell 
    } 
} 
+1

什麼阻止你添加圖片和描述?你有什麼問題? – rmaddy

+0

如果有更多的項目,圖像也會滾動嗎?如果不是,最簡單的方法是在'self.view'上添加一個UIImageView,它的原點是'self.tableView'的末尾。否則,你有關創建自定義'UITableViewCell'的問題?在'cellForRowAtIndexPath:'中管理各種'UITableViewCell'? – Larme

+0

@rmaddy我只是不知道如何添加圖片和描述... – user174782

回答

0

感謝@Larme所有幫助。添加以下代碼將能夠一個UIImage添加到標題(這意味着要實現TableHeaderView):

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 


     let logoView = UIImageView() 
     if section == 0 { 
      logoView.contentMode = .scaleAspectFit 
      let logo: UIImage = #imageLiteral(resourceName: "IMG_0517") 
      logoView.image = logo 
      view.addSubview(logoView) 
      return logoView 
     } else { 
      return nil 
     } 



    } 

    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
     if section == 0 { 
      return 200 
     } else { 
      return tableView.rowHeight 
     } 

    } 

下面是最終結果表明:

Final result picture

如果你想在UITableViewCell下添加一些文本,您可以添加一個頁腳到最後一節。

相關問題