Заполните UItableViewCell 2 массивом в быстрой - PullRequest
0 голосов
/ 20 декабря 2018

У меня есть базовый uitableview с 2 UITableViewCell. Я пытаюсь заполнить первую ячейку array1.count, а вторую - array2.count, но я не знаю как, это мой код:

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

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var value: Int = 0
        if(section == 0){
            value = 1
        }else if(section == 2){
            value = 5
        }
        return value
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.cell1, for: indexPath) as!  Cell1
            cell.lbl_cell1.text = "Subject"
            cell.selectionStyle = .none
            return cell
        } else if indexPath.row == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.cell2, for: indexPath) as!  Cell2
            cell.lbl_cell2.text = "Lorem Ipsum es simplemente el texto de relleno de las imprentas y archivos de texto. Lorem Ipsum ha sido el texto de relleno estándar de las industrias desde el año 1500, cuando un impresor (N. del T. persona que se dedica a la imprenta) desconocido usó una galería de textos y los mezcló de tal manera que logró hacer un libro de textos especimen."
            cell.selectionStyle = .none
            return cell
        }
        return UITableViewCell()
    }

Моя ошибка в том, что я всегда получаю 1 значение в каждой ячейке. Все, что мне нужно, это

Ячейка 1 Математическая ячейка 2 a, b, c, d, e, f, g, h .....

Заранее спасибо

Ответы [ 2 ]

0 голосов
/ 20 декабря 2018

Вы должны обработать это двумя разделами и добавить проверку для каждого раздела в методах UITableViewDataSource,

let numberOfSections = 2
var firstArray = [String]()
var secondArray = [String]()


override func viewDidLoad() {
    firstArray = ["1"]
    secondArray = ["a", "b", "c", "d", "e", "f"]
}


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

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if(section == 0) {
        return firstArray.count
    } else if(section == 1) {
        return secondArray.count
    }
    return 0
 }

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.cell1, for: indexPath) as!  Cell1
        let title = firstArray[indexPath.row]
        cell.lbl_cell1.text = title
        cell.selectionStyle = .none
        return cell
    } else if indexPath.section == 1 {
        let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.cell2, for: indexPath) as!  Cell2
        let title = secondArray[indexPath.row]
        cell.lbl_cell2.text = title
        cell.selectionStyle = .none
        return cell
    }
    return UITableViewCell()
}

Надеюсь, это поможет.

0 голосов
/ 20 декабря 2018

Попробуйте это:

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

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var value: Int = 0
    if(section == 0){
        value = array1.count
    }else{
        value = array2.count
    }
    return value
 }

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.cell1, for: indexPath) as!  Cell1
        item = array1[indexPath.row]
        ...
        return cell
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.cell2, for: indexPath) as!  Cell2
        item = array2[indexPath.row]
        ...
        return cell
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...