gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:48:43 25 4
gpt4 key购买 nike

我正在使用具有多个部分的表格 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com