TableView所有代理方法总结

2017-10-24  本文已影响0人  lalala1112389

1.设置区中的行数

 @available(iOS 2.0, *)
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if (section == 0){
            return 2
        }else if(section == 1){
            return 10
        }
        return 1
    }

2 区数

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

3 设置每一个区中每一行的行高

 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if(indexPath.section == 0 && indexPath.row == 0){
            return 100
        }else if(indexPath.section == 0 && indexPath.row == 1){
            return 44
        }else if(indexPath.section == 1){
            return 44
        }else if(indexPath.section == 2){
           return 120
        }
        return 0
    }

4 设置区头视图的高度

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
    {
        return 10
    }

5 设置区头视图的显示

 func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if (section == 1 || section == 2){
            
            let w = self.view.bounds.size.width
            self.headView = UIView(frame: CGRect(x: 0, y: 100, width: w, height: 10))
            self.headView.backgroundColor = UIColor.gray
            self.tableView.sectionHeaderHeight = 10
            return headView
        }
        return UIView()
     }

6 设置cellForRow
@available(iOS 2.0, *)
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
var result = UITableViewCell()
// let areaIdentifier:String = "AreaTableViewCell"
// let chaoXiangIdentifier:String = "ChaoXiangTableViewCell"
// let priceIdentifier:String = "PriceTableViewCell"
// let describeIdentifier:String = "DescribeTableViewCell"

  let topCell: TopTableViewCell = self.tableView.dequeueReusableCell(withIdentifier:topIdentifier ) as! TopTableViewCell
  let areaCell: AreaTableViewCell = self.tableView.dequeueReusableCell(withIdentifier:areaIdentifier ) as! AreaTableViewCell
  let chaoXiangCell: ChaoXiangTableViewCell = self.tableView.dequeueReusableCell(withIdentifier:chaoXiangIdentifier ) as! ChaoXiangTableViewCell
  let priceCell: PriceTableViewCell = self.tableView.dequeueReusableCell(withIdentifier:priceIdentifier ) as! PriceTableViewCell
  let describeCell: DescribeTableViewCell = self.tableView.dequeueReusableCell(withIdentifier:describeIdentifier) as! DescribeTableViewCell
   if(indexPath.section == 0 && indexPath.row == 0){
       //topCell.addSubview(collectionView)
        topCell.contentView.addSubview(collectionView)
        result = topCell
   }
   else if (indexPath.section == 0 && indexPath.row == 1){
        areaCell.leftLabel.text = "标题"
        areaCell.rightTextField.placeholder = "请输入标题"
        result = areaCell
   }else if(indexPath.section == 1 && indexPath.row == 0){
     areaCell.leftLabel.text = "面积"
     areaCell.rightTextField.placeholder = "请输入面积"
     result = areaCell
   }else if(indexPath.section == 1 && indexPath.row == 1){
     chaoXiangCell.chaoXIangLabel.text = "朝向"
     chaoXiangCell.chaoXiangBtn.setTitle("请输入朝向", for: .normal)
     chaoXiangCell.chaoXiangBtn.addTarget(self, action: #selector(ChangXiangBtnClick), for: .touchUpInside)
     chaoXiangCell.jianTouBtn.addTarget(self, action: #selector(ChangXiangBtnClick), for: .touchUpInside)
    result = chaoXiangCell
   }else if(indexPath.section == 1 && indexPath.row == 2){
     areaCell.leftLabel.text = "门牌号"
     areaCell.rightTextField.placeholder = "请输入门牌号"
     result = areaCell
    }else if(indexPath.section == 1 && indexPath.row == 3){
      priceCell.firstLabel.text = "价格"
      priceCell.secondLabel.text = "单价"
      priceCell.thirdLabel.text = "总价"
      priceCell.textField.placeholder = "请输入单价"
    
      priceCell.selectedBtn.tag = 0
      priceCell.unSelectedBtn.tag = 1
      priceCell.selectedBtn.isSelected = true
      priceCell.unSelectedBtn.isSelected = false
      priceCell.selectedBtn.addTarget(self, action: #selector(selectBtnClick(_:)), for: .touchUpInside)
      priceCell.unSelectedBtn.addTarget(self, action: #selector(selectBtnClick(_:)), for: .touchUpInside)
      result = priceCell
   }else if(indexPath.section == 1 && indexPath.row == 4){
    priceCell.firstLabel.text = "隔断"
    priceCell.secondLabel.text = "有"
    priceCell.thirdLabel.text = "无"
    priceCell.selectedBtn.tag = 0
    priceCell.unSelectedBtn.tag = 1
    priceCell.selectedBtn.isSelected = true
    priceCell.unSelectedBtn.isSelected = false
    priceCell.selectedBtn.addTarget(self, action: #selector(selectBtnClick(_:)), for: .touchUpInside)
    priceCell.unSelectedBtn.addTarget(self, action: #selector(selectBtnClick(_:)), for: .touchUpInside)
    priceCell.textField.placeholder = "请输入隔断数"
    priceCell.selectedBtn.isSelected = true
    priceCell.unSelectedBtn.isSelected = false
     result = priceCell
   }else if(indexPath.section == 1 && indexPath.row == 5){
     priceCell.firstLabel.text = "卫生间"
     priceCell.secondLabel.text = "有"
     priceCell.thirdLabel.text = "无"
     priceCell.selectedBtn.tag = 0
     priceCell.unSelectedBtn.tag = 1
     priceCell.selectedBtn.isSelected = true
     priceCell.unSelectedBtn.isSelected = false
     priceCell.selectedBtn.addTarget(self, action: #selector(weiShengJianClick(_:)), for: .touchUpInside)
     priceCell.unSelectedBtn.addTarget(self, action: #selector(weiShengJianClick(_:)), for: .touchUpInside)
     priceCell.textField.isHidden = true
     result = priceCell
   }else if(indexPath.section == 1 && indexPath.row == 6){
    areaCell.leftLabel.text = "容纳人数"
    areaCell.rightTextField.placeholder = "请输入大约容纳人数"
    result = areaCell
   }else if(indexPath.section == 1 && indexPath.row == 7){
     areaCell.leftLabel.text = "交通"
     areaCell.rightTextField.placeholder = "请输入交通位置"
     result = areaCell
   }else if(indexPath.section == 1 && indexPath.row == 8){
    areaCell.leftLabel.text = "具体地址"
    areaCell.rightTextField.placeholder = "请输入具体地址"
    result = areaCell
   }else if(indexPath.section == 1 && indexPath.row == 9){
    areaCell.leftLabel.text = "联系方式"
    areaCell.rightTextField.placeholder = "请输入联系方式"
    result = areaCell
   }else if(indexPath.section == 2 && indexPath.row == 0){
   
    result = describeCell
    }
    result.selectionStyle = .none
    return result
  
}

7 .cell侧滑删除

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
    {
        return true
    }
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
    {
        self.dataArray.remove(at: indexPath.row)
        self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.top)
        
    }
删除两字为英文
  func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle
    {
        return (UITableViewCellEditingStyle.delete)
    }
删除两字为中文
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
        return "删除"
 }

8 选中cell跳转

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        if indexPath.section == 1{
            let chooseSourceVC = ChooseSourceViewController()
            self.navigationController?.pushViewController(chooseSourceVC, animated: true)
        }
        
    }
上一篇下一篇

猜你喜欢

热点阅读