首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在selectedIndex扩展单元

在selectedIndex扩展单元
EN

Stack Overflow用户
提问于 2017-06-30 11:19:43
回答 2查看 344关注 0票数 3

更新代码(以下)

我正在处理的评论单元格限制在100个字符,如果它们包含更多的“显示更多的按钮”将出现。

如果按下,准确的单元格应该重新加载自己的行数更改为0,并充分显示该单元格,无论有多大。

我所取得的成就是,细胞重新加载,但不是选择的一个,有点随意。

下面是我的放大程序代码

注意:我的函数的更新集代码

问题:我必须按两次按钮才能得到结果,以最小化和最大化单元格。

代码语言:javascript
复制
   @IBAction func readMore(_ sender: UIButton) {


    self.state = !self.state

    print("state" , state)
    self.tapMore.setTitle(self.state ? self.decreaseState: self.expandState, for: .normal)
    self.commentLabel.numberOfLines = (self.state ? self.expandedLines: self.numberOfLines)
    print(self.commentLabel.numberOfLines)
    let myIndexPath = IndexPath(row: sender.tag, section: 0)

    UIView.animate(withDuration: 0.3, animations: {
        self.parentViewControllerCommentCell?.tableView.reloadRows(at: [myIndexPath], with: UITableViewRowAnimation(rawValue: Int(UITableViewAutomaticDimension))!)
    })
}

指数来源于

代码语言:javascript
复制
extension CommentTableViewCell {

var indexPath: IndexPath? {
    return (superview as? UITableView)?.indexPath(for: self)
   }
}

Note

print语句打印出选定的单元格(例如0、1或0,0 ),但它不会改变。

虽然我硬编码我的代码并更改了let myIndexPath =indexPath(行: indexPath!.row,节: 0)

让myIndexPath =IndexPath(行: 0,节: 0)

这个特性可以工作,但是可以任意地重新加载一些单元,任意地增大和减少单元。

在具有row: indexPath!.row的变量版本中,行状态也没有变化,而硬编码的行状态在3到0之间变化。

(谢谢你的帮助:)

加法

我的commentCell

代码语言:javascript
复制
class CommentTableViewCell: UITableViewCell {

@IBOutlet weak var likeCountButton: UIButton!
@IBOutlet weak var profileImageView: UIImageView!
@IBOutlet weak var commentLabel: KILabel!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var timeLabel: UILabel!
@IBOutlet weak var likeImageView: UIImageView!


@IBOutlet weak var tapMore: UIButton!

@IBOutlet weak var tapMoreButton: UIButton!


var delegate: CommentTableViewCellDelegate?
var postId : String!
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-06-30 12:20:49

这里有一种更好的方法可以为您提供正确的索引路径。首先,在cellForRow方法中,将当前索引行作为标记添加到“显示更多”按钮,然后将单击操作添加到按钮处理程序函数中。

在自定义的UIButton类中添加UITableViewCell的一个出口,如

代码语言:javascript
复制
class CustomCell: UITableViewCell {
     @IBOutlet var moreButton: UIButton! // Connect your button from storyboard
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell
    cell.moreButton.tag = indexPath.row
    /* Just add action normally from storyboard. No need to add target. cell.moreButton.addTarget(self, action:#selector(buttonUp(sender:)), for: .touchUpInside) */

    return cell
}

然后,在您的处理程序函数中,您可以通过读取这个标记来获得正确的索引路径。

代码语言:javascript
复制
func tapForMore(sender: UIButton) {
    let myIndexPath = IndexPath(row: sender.tag, section: 0)
    print("myindex", myIndexPath)
    //... other code here
}
票数 1
EN

Stack Overflow用户

发布于 2017-06-30 23:42:39

您使用类变量并跟踪点击计数。根据这些变量,您可以增加或减小单元格大小并重新加载它。

在YOURViewController中,将变量声明为:

代码语言:javascript
复制
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

       @IBOutlet weak var CommentsTableView: UITableView!
       var defaultSizeOfCell = 60.0
       var newSize = 80.0
       var selectedIndex = -1

       var isExpanded = false
       var expandCounter = 0
       override func viewDidLoad() { ...

将单元格中的按钮连接到此操作:

代码语言:javascript
复制
@IBAction func moreButtonAction(_ sender: UIButton) {

    if !isExpanded {
        if expandCounter == 0 {
            expandCounter = expandCounter + 1
        } else if expandCounter == 1 {
            expandCounter = 0
            isExpanded = true
            selectedIndex = sender.tag
            let myIndexPath = IndexPath(row: sender.tag, section: 0)

            UIView.animate(withDuration: 0.3, animations: {
                self.CommentsTableView.reloadRows(at: [myIndexPath], with: UITableViewRowAnimation(rawValue: Int(UITableViewAutomaticDimension))!)
            })
            print("Increase")
        }
    } else if isExpanded {
        if expandCounter == 0 {
            expandCounter = expandCounter + 1
        } else if expandCounter == 1 {
            expandCounter = 0
            isExpanded = false
            selectedIndex = -1
            let myIndexPath = IndexPath(row: sender.tag, section: 0)

            UIView.animate(withDuration: 0.3, animations: {
                self.CommentsTableView.reloadRows(at: [myIndexPath], with: UITableViewRowAnimation(rawValue: Int(UITableViewAutomaticDimension))!)
            })
            print("Decrease")
        }
    }


}

在tableview数据源函数中,向按钮添加标记:

代码语言:javascript
复制
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "testCell", for: indexPath) as! TestTableViewCell
    cell.moreButton.tag = indexPath.row
    return cell
}

最后,为单元格的高度添加此委托方法:

代码语言:javascript
复制
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if selectedIndex == indexPath.row {
        return CGFloat(newSize)
    } else {

        return CGFloat(defaultSizeOfCell)

    }

}

更不用说,按钮应该在单元格中并连接到YOURCustomTableViewCell类,如下所示:

代码语言:javascript
复制
class TestTableViewCell: UITableViewCell {

      @IBOutlet weak var moreButton: UIButton!

我已经根据你的要求测试过了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44845247

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档