如何在具有多个部分的表视图单元格中嵌入文本视图?

时间:2016-12-23 06:07:44

标签: ios swift uitableview textview

我正在使用包含多个部分的表格视图。在一个表视图单元格中,该单元格具有文本标签和详细文本标签以显示数组对象。现在我想添加一个新的部分,在本节中我将有更多的文字像一个段落。如何在文本视图中显示数据?

{
var dataSection01:NSMutableArray = NSMutableArray()
var dataSection02:NSMutableArray = NSMutableArray()
var sectionTitleArray : NSMutableArray = NSMutableArray()
var arrayForBool : NSMutableArray = NSMutableArray()
var sectionContentDict : NSMutableDictionary = NSMutableDictionary()

in viewDidLoad()
sectionTitleArray = ["Assignment Information","Details"]
let tmp1 : NSArray = assignInfoArray
var string1 = sectionTitleArray .objectAtIndex(0) as? String
[sectionContentDict .setValue(tmp1, forKey:string1! )]
let tmp2 : NSArray = detailsInfoArray
string1 = sectionTitleArray .objectAtIndex(1) as? String
[sectionContentDict .setValue(tmp2, forKey:string1! )]

dataSection01 = [ "Name:", "Phone", "Email" so on]
dataSection02 = [ "Address", "Street", "State","ZipCode"]

//mapping like this using object mapper
assignInfoArray.addObject((newDetails?.clientName)!)
assignInfoArray.addObject((newDetails?.clientPhone)!)


func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return sectionTitleArray.count
}
  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{



    let CellIdentifier = "CellRightDetail"
    var cell :UITableViewCell?
    cell = self.tableView.dequeueReusableCellWithIdentifier(CellIdentifier)
    if cell == nil {
        cell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: "CellRightDetail")
    }
    let manyCells : Bool = arrayForBool .objectAtIndex(indexPath.section).boolValue

    if (!manyCells) {
        //  cell.textLabel.text = @"click to enlarge";
    }
    else{
        let items: NSMutableArray = self.dataFromIndexPath(indexPath)

        let content = sectionContentDict .valueForKey(sectionTitleArray.objectAtIndex(indexPath.section) as! String) as! NSArray
        cell!.detailTextLabel?.text = content .objectAtIndex(indexPath.row) as? String
        cell!.detailTextLabel?.textColor = UIColor.whiteColor()
        cell!.textLabel?.textColor = UIColor.whiteColor()
        cell!.textLabel?.font = UIFont(name:"Helvetica-Bold", size: 13.0) 
        cell!.textLabel?.text = items[indexPath.row] as? String

    }

    return cell!
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

    if(arrayForBool .objectAtIndex(section).boolValue == true)
    {
        let tps = sectionTitleArray.objectAtIndex(section) as! String
        let count1 = (sectionContentDict.valueForKey(tps)) as! NSArray
        return count1.count
    }
    return 0;
}
func dataFromIndexPath(indexPath: NSIndexPath!) -> NSMutableArray!
{
    if (indexPath.section == 0)
    {
        return dataSection01
    }
    else if (indexPath.section == 1)
    {
        return dataSection02
    }

    return nil
}

}

1 个答案:

答案 0 :(得分:0)

在故事板中 蓝色是第一个细胞,绿色是我的第二个细胞。

enter image description here

您必须创建两个这样的单元格,并且每个单元格都必须创建不同的customcell类。在各自的班级中设置UILabelUITextView的出口,并使用以下代码。 可能是你的要求变化不大,所以请根据它使用代表。

像这样实施您的代码:

// MARK:- --- TableView Height ---

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

    open func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat{
        return 1
    }

    open func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 45
    }

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

    // MARK:- --- TableView tittle ---
     open func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?{

        return "hearder " + String(section)
    }

    open func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?{

        let viewHeader : UIView = UIView(frame: CGRect(x: 0 , y: 64, width: self.view.frame.size.width , height: 45))
        viewHeader.backgroundColor = UIColor(red: 180.0/255.0, green: 160.0/255.0, blue: 240.0/255.0, alpha: 1)
        let btnHeader : UIButton = UIButton(frame: CGRect(x: 0 , y: 0, width: viewHeader.frame.size.width , height: viewHeader.frame.size.height))
        btnHeader.setTitle("section #\(section)", for: UIControlState())
        btnHeader.backgroundColor = UIColor.darkGray
        btnHeader.isSelected = arrIsExpand[section]
        btnHeader.tag = section
        btnHeader.addTarget(self, action:#selector(expandMethod(_:)) , for: UIControlEvents.touchUpInside)
        viewHeader.addSubview(btnHeader)

        return viewHeader

    }

    // MARK:- --- TableView data source ---
    open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
          return 10

    }

    open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

        if(indexPath.section == 0){ // for your first section
            let firstCell : MyFirstCustomCell = tableView.dequeueReusableCell(withIdentifier: "firstcell") as! DesignTableCell
            firstCell.lblTittle?.text = "Tittle"
            firstCell.lblDescription?.text = "Description"
             return firstCell
        }
            //for second section
        else{
            let secondCell : MySecondCustomCell = tableView.dequeueReusableCell(withIdentifier: "secondcell") as! DesignTableCell
            secondCell.lblTittle?.text = "Tittle"
            secondCell.txtViewData?.text = "Blah! Blah!"
            return secondCell
        }
    }

你可能面对行高的问题,因为我在这里给出静态高度,即45,所以使用uiautomaticdimension而不是静态45,并且还要委托代理estimatedHeightForRow

相关问题