Как я могу увеличить высоту для конкретной ячейки в определенном разделе - PullRequest
0 голосов
/ 10 июня 2018

Как я могу увеличить высоту для определенной ячейки (в UITableView) в определенном разделе (второй раздел в моем случае, где это изображение со Swift).Я не знаю, возможно ли установить автоматическое изменение размера моей ячейки, но на данный момент я хочу только увеличить размер этой ячейки, потому что внутри я ничего не вижу.Мое приложение выглядит так: Cell is too small

Я хочу увеличить размер той ячейки, в которой находится WebView.

Вот мой код:

extension DetailsViewController: UITableViewDelegate, UITableViewDataSource{

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

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

        switch section {
        case 0: return 6
        case 1: return 1
        default:
            return 0
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        switch indexPath.section{
        // Section 1: Repository Details
        case 0:

            let cell = detailsTableView.dequeueReusableCell(withIdentifier: "repoDetailsCell", for: indexPath) as! DetailsTableViewCell

            // Set each row from first section
            switch indexPath.row{

            case 0:

                cell.avatarImageView.downloadedFrom(link: "\(selectedRepo.owner.avatarURL)")

                if indexPath.row == 0{
                    self.detailsTableView.rowHeight = 60
                }

                cell.textLabel?.isHidden = true
                cell.detailTextLabel?.text = selectedRepo.description
                cell.textLabel?.numberOfLines = 0
                cell.detailTextLabel?.numberOfLines = 0
                return cell
            case 1:
                cell.textLabel?.text = selectedRepo.description
                cell.textLabel?.numberOfLines = 0
                cell.detailTextLabel?.isHidden = true
                return cell
            case 2:
                cell.textLabel?.text = "Open Issues"
                cell.detailTextLabel?.text = String(selectedRepo.openIssuesCount)
                return cell
            case 3:
                cell.textLabel?.text = "Forks"
                cell.detailTextLabel?.text = String(selectedRepo.forksCount)
                return cell
            case 4:
                cell.textLabel?.text = "Watchers"
                cell.detailTextLabel?.text = String(selectedRepo.watchersCount)
                return cell
            case 5:
                cell.textLabel?.text = "URL"
                cell.detailTextLabel?.text = selectedRepo.htmlURL
                cell.detailTextLabel?.textColor = UIColor.blue
                cell.detailTextLabel?.numberOfLines = 0
                return cell
            default:
                return cell

            }

        // Section 2: Webview
        case 1:

            let cell = tableView.dequeueReusableCell(withIdentifier: "repoWebviewCell", for: indexPath) as! DetailsTableViewCell

            // Set the WebView from Section 2
            switch indexPath.row{
            case 0:

                UIWebView.loadRequest(cell.webView)(NSURLRequest(url: NSURL(string: "\(selectedRepo.htmlURL)/blob/master/README.md")! as URL) as URLRequest)


                return cell
            default:
                return cell
            }

        default: return UITableViewCell()
        }
    }

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 200
    }


}

Ответы [ 2 ]

0 голосов
/ 10 июня 2018

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

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    switch (indexPath.section, indexPath.row) {
     case (1, 0):
         return 200
     case (0, _):
         return 60
     default: 
         return 60 //some default value
    }
}
0 голосов
/ 10 июня 2018

Реализуйте метод делегата heightForRowAt.

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.section == someSection && indexPath.row == someRow {
        return someBiggerHeight
    } else {
        return tableView.rowHeight // return the standard height for all others
    }
}

Если вам нужно проверить несколько путей индекса, тогда использовать switch проще:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    switch indexPath {
    case [someSection, someRow]:
        return someBiggerHeight
    case [someOtherSection, someOtherRow]:
        return someOtherBiggerHeight
    default:
        return tableView.rowHeight // return the standard height for all others
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...