如何从INDEXPATH.ROW

时间:2017-03-06 14:50:07

标签: ios swift uitableview

我得到了这样的API回复:

[{
  "name":"apple"
   "type": "Fruit"
     "taste": sweet

    }
  {
    "name":"lemon"
   "type": "Fruit"
 "taste": "not tasty"
 }]

现在我想将该部分的标题设置为UITable视图的名称

为此我在我的视图控制器中将其称为:

 var modelclass = [FruitsModelclass]()

 override func viewDidLoad()
{
self.loaddata()
}

func loaddata()
{
 //here i have called the API and sucessfully get the response in data and assign as

    self.modelclass = data
    self.tableView.reloadData()
    }

 override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
  let cell = tableView!.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath!) as FruitCell
    let model = self.Modeldata[indexpath.row]
     cell.textlabel.text = model.name
    return cell
}

这就是我如何将名称设置为cell.How我可以将它设置为我的headerSection .. ??我的Api中有很多json对象,而不是固定的。在这里我只提出了两个可以帮助我的人...... ??

1 个答案:

答案 0 :(得分:0)

如果你只想添加标题......

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "SOME TEXT" //return your text here, may be 
}

如果你想控制标题视图,那就像这样

    public func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat{
        return 50 //height you want of section
    }
    public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?{

        let vw = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 50)) 
//view for header, you can also use view from nib, but thats another matter
        vw.backgroundColor = UIColor.white

        let label = UILabel(frame: CGRect(x: 10, y: 0, width: tableView.frame.size.width-10, height: 50)) 
//add label or buttons or what ever u want to view....
        label.text = “SOME TEXT“

        vw.addSubview(label)

        return vw
    }

作为旁注,numberOfSections显示标题