Как уменьшить высоту ячейки в табличном представлении iOS swift? - PullRequest
0 голосов
/ 06 июня 2018

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

Вот код, который я пробовал:

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

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

    guard posts.count > 0 else {
        return cell
    }

    cell.post = posts[indexPath.row]
    cell.homeVC = self
    cell.delegate = self
    cell.profileImageView.tag = indexPath.row
    cell.postImageView.tag    = indexPath.row
    cell.shareImageView.tag   = indexPath.row
    cell.nameLabel.tag        = indexPath.row
    cell.productRatingLabel.tag = indexPath.row
//        cell.postTime.tag =
//        cell.postTime.tag =
    cell.postTime.tag = indexPath.row
    cell.locationName.tag = indexPath.row

    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    let cell = tableView.dequeueReusableCell(withIdentifier: "HomeCell", for: indexPath) as! HomeTableViewCell

    if cell.postImageView == nil {
        cell.postImageView.isHidden = true
        cell.locationName.isHidden = false

        return 100
    } else {
        cell.postImageView.isHidden = false
        cell.locationName.isHidden = true

        return 498
    }
}

excepted result

это то, что я хочу ..

Ответы [ 2 ]

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

Альтернативный раствор

cell 1: cellwithImage

cell 2: cellWithoutImage

based on imageUrl is nill than display cellwithoutImagecell 
based on imgaeUrl is there than display cellwithImage
0 голосов
/ 06 июня 2018

Этого можно добиться, выполнив следующие действия:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    let post = posts[indexPath.row]
    if let url = post.imageURL {
        return 498
    } else {
        return 100
    }
}

Теперь спрячьте и покажите свои imageView и locationName в вашем методе cellForRowAt indexPath.

FYI. Кодне проверено, это просто информация, с помощью которой вы можете достичь своей цели.

Дайте мне знать в случае любого запроса.

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