ios - 如何将 TextView 嵌入具有多个部分的 TableView 单元格内?

标签 ios swift uitableview textview

我正在使用具有多个部分的表格 View 。在一个 TableView 单元格中,有文本标签和详细文本标签来显示数组对象。现在我想添加一个新部分,在本部分中我将有更多文本,例如段落。如何在 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
}

}

最佳答案

在 Storyboard中 蓝色是第一个单元格,绿色是我的第二个单元格。

enter image description here

您必须像这样创建两个单元格,并且必须为每个单元格创建不同的自定义单元格类。在各自的类中创建 UILabelUITextView 的导出并使用以下代码。 可能您的要求略有不同,因此请根据它使用委托(delegate)。

像这样实现您的代码:

// 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,并实现委托(delegate)estimatedHeightForRow .

关于ios - 如何将 TextView 嵌入具有多个部分的 TableView 单元格内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41296029/

相关文章:

swift - 快速获取月份组件的每个名称

ios5 - ios 5 UISearchDisplayController 崩溃

cocoa-touch - UITableViewCell的accessoryView在可见 View 改变后消失

iphone - 如何在ios中将设备高度#define为宏

ios - 在 Swift 中将整数映射到字符串

ios - 将字符串添加到另一个字符串的开头

ios - 图像缩放,质量不松动

ios - 如何使用 Swift 5 在 SwiftUI 中使用具有 RGB 或十六进制值的颜色?

ios - 如何在 Swift 中找到 NSDocumentDirectory?

Swift - 编译器警告我没有解包一个可选的但然后强制我解包使用!而不是?