iOS实战【好书】之评论栏

建立cell

tableView的cell效果如下:


iOS实战【好书】之评论栏_第1张图片
image.png

为了建立该cell,我们需要自定义头像UIImage,姓名、日期以及评论区域三个label:

     self.avatarImage = UIImageView(frame: CGRectMake(8,8,40,40))
            self.avatarImage?.layer.cornerRadius = 20
            self.avatarImage?.layer.masksToBounds = true
            self.contentView.addSubview(self.avatarImage!)
            
            self.nameLabel = UILabel(frame: CGRectMake(56,8,SCREEN_WIDTH-56-8,15))
            self.nameLabel?.font = UIFont(name: MY_FONT, size: 13)
            self.contentView.addSubview(self.nameLabel!)
            
            
            self.dateLabel = UILabel(frame: CGRectMake(56,self.frame.size.height-8-10,SCREEN_WIDTH-56-8,10))
            self.dateLabel?.font = UIFont(name: MY_FONT, size: 13)
            self.dateLabel?.textColor = UIColor.grayColor()
            self.contentView.addSubview(self.dateLabel!)
            
            self.detailLabel = UILabel(frame: CGRectMake(56,30,SCREEN_WIDTH-56-8,self.frame.size.height - 30 - 25))
            self.detailLabel?.font = UIFont(name: MY_FONT, size: 15)
            self.detailLabel?.numberOfLines = 0
            self.contentView.addSubview(self.detailLabel!)

为了实现评论区高度自适应,还需添加如下语句,用于定义每个cell的高度:

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        let object = self.dataArray[indexPath.row] as? AVObject
        let text = object!["text"] as? NSString
        let textSize = text?.boundingRectWithSize(CGSizeMake(SCREEN_WIDTH-56-8,0), options: .UsesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(15)], context: nil).size
        
        return (textSize?.height)! + 30 + 25
    }

注意运行顺序:
1、调用PresentViewController时,是在初始化VC时就初始化Viewdidload;该错误导致tableView无显示;
2、cell在执行tableView之前执行,那么tableView自定义高度变得无法实现;需要在执行定义tableview cell时加载cell初始化;不然会导致cell高度错误

上拉与加载

为了使tableview能实现上拉与加载需要在viewdidload时添加上拉与加载的响应:

        self.tableView?.mj_header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: "headerRefresh")
        self.tableView?.mj_footer = MJRefreshBackNormalFooter(refreshingTarget: self, refreshingAction: "footerRefresh")

然后从LeanCloud中得到数组,并对数组排序;limit是限制一个tableview显示多少个数据,skip表示一次跳过多少个数据;whereKey表示依据什么排序,最后把排序好的dataarray加载进数组并reload tableview:

/**
    *  上拉加载、下拉刷新
    */
    func headerRefresh(){
        let query = AVQuery(className: "discuss")
        query.orderByDescending("createdAt")
        query.limit = 20
        query.skip = 0
        query.whereKey("user", equalTo: AVUser.currentUser())
        query.whereKey("BookObject", equalTo: self.BookObject)
        query.includeKey("user")
        query.includeKey("BookObject")
        query.findObjectsInBackgroundWithBlock { (results, error) -> Void in
            self.tableView?.mj_header.endRefreshing()
            
            self.dataArray.removeAllObjects()
            self.dataArray.addObjectsFromArray(results)
            self.tableView?.reloadData()
        }
    }
    func footerRefresh(){
        let query = AVQuery(className: "discuss")
        query.orderByDescending("createdAt")
        query.limit = 20
        query.skip = self.dataArray.count
        query.whereKey("user", equalTo: AVUser.currentUser())
        query.whereKey("BookObject", equalTo: self.BookObject)
        query.includeKey("user")
        query.includeKey("BookObject")
        query.findObjectsInBackgroundWithBlock { (results, error) -> Void in
            self.tableView?.mj_footer.endRefreshing()
            self.dataArray.addObjectsFromArray(results)
            self.tableView?.reloadData()
        }
    }

评论栏InputView

为了在下方添加一个评论栏,需要加载一个第三方库InputView,并ViewController中添加进去:

      self.input = NSBundle.mainBundle().loadNibNamed("InputView", owner: self, options: nil).last as? InputView
        self.input?.frame = CGRectMake(0,SCREEN_HEIGHT-44,SCREEN_WIDTH,44)
        self.input?.delegate = self
        self.view.addSubview(self.input!)

为了实现InputView功能,需要实现InputView相关的代理方法,譬如:textViewHeightDidChange、textViewHeightDidChange、keyboardWillHide以及keyboardWillHide;实现键盘的出现和隐藏以及高低变化,在这个观察中Input的view相应bottom也会随之改变。
为了实现评论完后,InputView复原;还需要在keyboardWillHide中添加如下代码:

   func keyboardWillHide(inputView: InputView!, keyboardHeight: CGFloat, animationDuration duration: NSTimeInterval, animationCurve: UIViewAnimationCurve) {
        UIView.animateWithDuration(duration, delay: 0, options: .BeginFromCurrentState, animations: { () -> Void in
            self.layView?.alpha = 0
            self.input?.bottom = SCREEN_HEIGHT
            }) { (finish) -> Void in
                self.layView?.hidden = true
                self.input?.resetInputView()
                self.input?.inputTextView?.text = ""
                self.input?.bottom = SCREEN_HEIGHT
        }   
    }

你可能感兴趣的:(iOS实战【好书】之评论栏)