2016-12-23 71 views
0

我正在使用多個部分的表格視圖。在一個表格中查看具有文本標籤和詳細文本標籤以顯示數組對象的單元格。現在我想添加一個新的部分,在這一部分中,我將有更多的文字像段落。如何在文本視圖中顯示數據?如何在包含多個部分的表格視圖單元格中嵌入文本視圖?

{ 
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 
} 

} 
+0

你能提供一個想象,顯示你想要達到的目標嗎? – Erik

+0

@Ajay_Ajju,你應該編輯你的問題,向我們展示更多信息,你的問題不清楚,顯示你的代碼。 – aircraft

+0

我認爲創建CustomTableViewCell是很好的答案 –

回答

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,也impliment的代表estimatedHeightForRow

+0

非常感謝它 –

+0

歡迎@Ajay_Ajju –

相關問題