2017-02-01 6 views
3

그래서 다른 이미지를 tableview 셀에 넣고 싶습니다. 3 개의 다른 파일이 있습니다. 이미 코딩을했는데 문제가 있습니다. 제발, 제 코드를보고이 마약 쟁이를 다루는 법을 가르쳐주세요. (하루 종일 꼼짝하지 마라.) ... 이것의 요지는 사용자 정의 셀에 다른 이미지와 라벨 두 개가있는 것입니다.테이블보기 다른 데이터가있는 사용자 정의 셀

먼저 파일

class MenuCells { 

private var _cellTitle: String 
private var _cellDetails: String 
private var _cellIcon: Array<String> 

var cellTitle: String { 
    return _cellTitle 
} 

var cellDetails: String { 
    return _cellDetails 
} 

var cellIcon: Array<String> { 
    return _cellIcon 
} 

init(cellTitle: String, cellDetails: String, cellIcon: Array<String>) { 

    _cellTitle = cellTitle 
    _cellDetails = cellDetails 
    _cellIcon = cellIcon 

} 

func cellData() { 
    _cellTitle = "x" 
    _cellDetails = "y" 
    _cellIcon = ["1","2","3","4"] 
} 

} 

두 번째 파일

class MenuCell: UITableViewCell { 


@IBOutlet weak var bg: UIView! 
@IBOutlet weak var cellTitle: UILabel! 
@IBOutlet weak var cellDetails: UILabel! 
@IBOutlet weak var cellIcon: UIImageView! 


override func awakeFromNib() { 
    super.awakeFromNib() 


} 

func configureCell(menuCell: MenuCells) { 

    cellTitle.text = menuCell.cellTitle 
    cellDetails.text = menuCell.cellDetails 
    cellIcon.image = UIImage(named: "\(menuCell.cellIcon)") 

} 
} 

세 번째 파일 (여기서 임 붙어, 나는 데이터를 구현하는 방법을 잘 모릅니다)

import UIKit 

class MenuVC: UIViewController, UITableViewDelegate, UITableViewDataSource { 


@IBOutlet weak var tableMenu: UITableView! 



override func viewDidLoad() { 
    super.viewDidLoad() 

    tableMenu.delegate = self 
    tableMenu.delegate = self 



} 

func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 4 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "menuCell", for: indexPath) as? MenuCell 


    return cell 
} 


} 
+0

코드가 완벽합니다. 거의 –

+0

viewDidLoad는'tableMenu.delegate = self'를 두 번 설정하고 그 모양으로는 dataSource를 설정하지 않았습니다. 해당 라인 중 하나를'tableMenu.dataSource = self'로 대체하거나 연결 관리자를 통해 스토리 보드의 ViewController에 연결하여 dataSource를 설정해보십시오. –

답변

1

이렇게해야합니다.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
let cell = tableView.dequeueReusableCell(withIdentifier: "menuCell", for: indexPath) as? MenuCell 

// create object of MenuCells file and assign your variables.Instead of creating array of images in MenuCell, create array in this file and send image as a param 1-by-1. 
let myMenuCells = MenuCells(cellTitle: "Title", cellDetails: "Details", cellIcon: image) 

//By this below method set your data in your outlets. As this func is already doing so call it. 
cell.configureCell(menuCell: myMenuCells) 

return cell 
    } 

당신이 당신의 콘센트에 이미지 이름과 func configureCell 자동 할당 이미지를 전송하기 때문에 private var _cellIcon: String

  • private var _cellIcon: Array<String>을 변경합니다. 당신이 당신의 스토리 보드에 프로토 타입 셀이있는 경우

  • cellIcon.image = UIImage(named: "\(menuCell.cellIcon)")cellIcon.image = UIImage(named: menuCell.cellIcon)

0

에, 당신은 당신의 프로토 타입 셀 각각에 식별자를 설정할 수 있고, 문은 코드 실행을 할 경우 각 셀에 대해 개별적으로 사용합니다. (식별자가 셀 A의 식별자인지 확인하십시오. 그렇다면 셀 A의 설정을 실행하고 셀 B의 설정을 실행하십시오).

+0

조언을 해주셔서 감사합니다. 지금은 4 셀만 필요하기 때문에 생각하고 있었지만 이후에는 훨씬 더 많은 것을 필요로 할 것입니다. 현재 아무 세포도 나타나지 않고 있습니다. 이유가 있습니까? –

+0

귀하의 viewDidLoad를 추측하십시오. tableMenu.delegate = self를 두 번 설정했습니다. 데이터 소스를 설정해야합니다. –

+0

감사합니다, 덕분에,이 하나를 보았습니다 : D : D –