美文网首页
UITableView篇

UITableView篇

作者: 时无寻 | 来源:发表于2020-04-22 23:22 被阅读0次

通过cell的frame设置卡片样式

override var frame: CGRect {
        didSet {
            var f = frame
            f.origin.x = 8
            f.size.width = XFScreen.w - 16
            super.frame = f
        }
    }

改变tableViewHeaderHeaderView或tableViewHeaderFooterView的颜色

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        view.tintColor = UIColor.clear
    }

func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
        view.tintColor = UIColor.clear
    }

全局设置

UITableViewHeaderHeaderView.appearance().tintColor = UIColor.clear
UITableViewHeaderFooterView.appearance().tintColor = UIColor.clear

全选

// 需要设置这个属性才可出现打勾按钮
tableView.allowsMultipleSelectionDuringEditing = true

编缉

// 添加编缉按钮
navigationItem.rightBarButtonItem = editButtonItem

// 事件响应
 override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
        tableView.setEditing(editing, animated: animated)
    }
  
// talbeViewDelegate  
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { return true }
                        
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
    return UITableViewCellEditingStyle.delete
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == UITableViewCellEditingStyle.delete {
        
    }
}

自定义删除

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { return true }

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    let action = UITableViewRowAction(style: .normal, title:"删除") { [weak self] action, indexPath in
       
    }
    action.backgroundColor = UIColor.red
    return [action]
}

处理键盘消失

@available(iOS 7.0, *)
public enum UIScrollViewKeyboardDismissMode : Int {
    case none
    case onDrag // dismisses the keyboard when a drag begins
    case interactive // the keyboard follows the dragging touch off screen, and may be pulled upward again to cancel the dismiss
}

UITableViewCell 处于选中状态,UIView 的背景颜色消失

override func setHighlighted(_ highlighted: Bool, animated: Bool) {
    let myViewBackgroundColor = myView.backgroundColor
    super.setHighlighted(highlighted, animated: animated)
    myView.backgroundColor = myViewBackgroundColor
}

override func setSelected(_ selected: Bool, animated: Bool) {
    let myViewBackgroundColor = myView.backgroundColor
    super.setSelected(selected, animated: animated)
    myView.backgroundColor = myViewBackgroundColor
}

取消headerview的悬停效果

// MARK: - UIScrollViewDelegate
extension GoodsInfoViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView == _tableView {
            let sectionHeaderHeight = 10
            if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
                scrollView.contentInset = UIEdgeInsets(top: -scrollView.contentOffset.y, left: 0, bottom: 0, right: 0);
            } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
                scrollView.contentInset = UIEdgeInsets(top: -sectionHeaderHeight, left: 0, bottom: 0, right: 0);
            }
        }
    }
}

tableHeaderView 高度自适应(适用约束布局)

// 获取headerView高度
self.headerView.height = self.headerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height
// 设置
 self._tableView.tableHeaderView = self.headerView

UITableViewCell直接获取identifier

 extension UITableViewCell {
    static var identifier: String {
        return NSStringFromClass(self)
    }
}

使用

var cell = tableView.dequeueReusableCell(withIdentifier: UITableViewCell.identifier) as? UITableViewCell
if cell == nil {
  cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: UITableViewCell.identifier)
}

相关文章

网友评论

      本文标题:UITableView篇

      本文链接:https://www.haomeiwen.com/subject/gzciihtx.html