2016-06-28 4 views
0
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("idcell", forIndexPath: indexPath) as UITableViewCell 
    let lblTitle : UILabel = cell.contentView.viewWithTag(101) as! UILabel 
    lblTitle.text = (deptId[indexPath.row] as? String)! + "  " + (deptDesc[indexPath.row] as? String)! 
    var height:CGFloat = 0 
    cell.backgroundColor = UIColor.whiteColor() 
    cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator 
    if(indexPath == selectedIndexPath){ 
     cell.backgroundColor = UIColor.grayColor() 
     for i in 0...deptProfile.count-1{ 
      let deptmentProfile = UIButton(frame: CGRectMake(0,44+height,400,41)) 
      deptmentProfile.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside) 
      height = height+41 
      deptmentProfile.setTitle(deptProfile[i] as! String, forState: UIControlState.Normal) 
      deptmentProfile.setTitleColor(UIColor.blackColor(), forState: .Normal) 
      deptmentProfile.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left 
      deptmentProfile.backgroundColor = UIColor.whiteColor() 
      deptmentProfile.titleEdgeInsets = UIEdgeInsetsMake(0, 40, 0, 0); //margin to the left 
      cell.addSubview(deptmentProfile) 
     } 
     cell.accessoryType = UITableViewCellAccessoryType.None 

    } 
    return cell 
} 

質問:展開されたデータが動的なので、deptProfrileは動的です。どのようにして細胞の内容をクリアすることができますか?サブビューは毎回セルに残っているようです。動的データを含む展開可能なセル

答えて

0

セルが再利用されているので、elseの場合はifif(indexPath == selectedIndexPath)というようにコード化していないことに注意してください。そのelseのUIのコードを入力する必要があります。

テーブルビューの推定セル高さを指定すると、セルが展開されます。

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
{ 
    if(indexPath == selectedIndexPath){ 
     var cellHeight = 100 // Assume this is the height when deptProfile.count = 0 
     cellHeight = cellHeight + 44 * deptProfile.count 
     return cellHeight 
    } 
    return super.tableView(tableView, heightForRowAtIndexPath: indexPath) 
} 
関連する問題