Поместите UIButton в tableView - PullRequest
0 голосов
/ 30 января 2019

Я сделал код для добавления массива UIView в мой TableView, но когда я их добавляю, он не показывает их по строкам, пока я сделал цикл для добавления каждого элемента в мой TableView, но он добавляет иходно над другим, другое решение?

контроллер моего вида:

class mainViewController: UIViewController {

  private var myArray: [UIView] = []
  private var myTableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    let barHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height
    let displayWidth: CGFloat = self.view.frame.width
    let displayHeight: CGFloat = self.view.frame.height

    myTableView = UITableView(frame: CGRect(x: 50, y: barHeight, width: displayWidth, height: displayHeight - barHeight))
    myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
    myTableView.dataSource = self
    myTableView.delegate = self
    self.view.addSubview(myTableView)

    let DoneBut: UIButton = UIButton(frame: CGRect(x: 50, y: 0, width: 150, height: 50))
    DoneBut.setTitle("Done", for: .normal)
    DoneBut.backgroundColor = UIColor.blue

    let DoneBut2: UIButton = UIButton(frame: CGRect(x: 50, y: 0, width: 50, height: 50))
    DoneBut2.setTitle("Done2", for: .normal)
    DoneBut2.backgroundColor = UIColor.blue

    let view1 = UIView()
    view1.addSubview(DoneBut)
    myArray.append(view1)

    let view2 = UIView()
    view2.addSubview(DoneBut2)
    myArray.append(view2)
}

}

extension mainViewController: UITableViewDataSource {
 func tableView(_ tableView: UITableView, cellForRowAt indexPath: 
  IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: 
    "MyCell", for: indexPath as IndexPath)
    for array in myArray {
        cell.contentView.addSubview(array)
    }
    return cell
 }

 func tableView(_ tableView: UITableView, numberOfRowsInSection 
 section: Int) -> Int {
    return myArray.count
 }
}
extension mainViewController: UITableViewDelegate {
 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: 
 IndexPath) {
    print("Num: \(indexPath.row)")
    print("Value: \(myArray[indexPath.row])")
 }
}

1 Ответ

0 голосов
/ 30 января 2019

Ваша проблема в том, что каждый раз, когда функция cellForRowAt запрашивает ячейку, вы просматриваете ВСЕ представления и добавляете их в каждую ячейку.Вместо этого вы должны индексировать его, используя indexPath.Смотрите следующее:

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

    // check if there is a view at the indexPath
    if indexPath.row < myArray.count {
        // there is a view, add it to the cells contentView
        cell.contentView.addSubview(myArray[indexPath.row])
    } else {
        print("no view at index")
    }

    return cell
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...