2017-10-04 9 views
0

UITableViewCell에서 하나의 그림과 일부 텍스트를 추가하고 싶습니다. 나는 순수한 코드로있는 UITableViewCell을 구현할 수프로그래밍 방식으로 UITableViewCell에서 UITextView 및 UIImageView 추가 프로그래밍 방식으로 Swift 4

Here is what I want to achieve

그러나 : 여기에 내가 달성하고자하는 결과이다. 누구든지 UITableViewCell 아래 또는 위에 그림 또는 일부 텍스트를 추가하는 방법을 알고 있습니까? 모든 제안을 부탁드립니다.

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

더 많은 항목이 있다면 이미지도 스크롤됩니까? 없다면, 가장 간단한 방법은 UIImageView를'self.view'에 추가하는 것입니다.이 곳의 원점은'self.tableView'의 끝 부분입니다. 그렇지 않으면 커스텀'UITableViewCell'을 만드는 것에 관한 문제입니까? 'cellForRowAtIndexPath :'에서 다양한 종류의'UITableViewCell'을 관리하고 있습니까? – Larme

+0

@rmaddy 그림과 설명을 추가하는 방법을 모르겠다. ... – user174782

답변

0

감사 @Larme 모든 도움 : 여기

My current result

내 코드입니다 : 여기에 내 현재의 결과입니다. 추가 다음과 같은 코드가 (A TableHeaderView 구현하는 의미) 헤더에있는 UIImage를 추가 할 수 있습니다 : 여기

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 아래에 텍스트를 추가하려면 마지막 섹션에 꼬리말을 추가하면됩니다.