我正在使用Xib文件而不是故事板。我用一些虛擬數據創建了一個表格視圖。它的工作正常,現在我已經創建了一些帶有一些標籤和textFields的自定義單元格。我怎樣才能使用它作爲UITableView的頁眉或頁腳?如何將自定義單元格設置爲UITableView的頁眉或頁腳
4
A
回答
1
您可以編程方式創建自定義視圖。我試了下面的代碼。嘗試一次。
override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let customview:UIView = UIView();
let label = UILabel();
label.text = "ur custom data"
/*
You can add any number of subviews you want here
And don't forget to add it to your customview.
*/
customview.addSubview(label)
return customview;
}
同樣,你也可以爲標題做同樣的事情。
1
這是方法添加TableView的標題: 在那你可以任何控制像UIButton,UIImageView,....等。
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionView = UIView()
sectionView.frame = CGRectMake(x,y,height,width) // For Set the Frame
sectionView.backgroundColor = UIColor.redColor()
return sectionView
}
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let sectionView = UIView()
sectionView.frame = CGRectMake(x,y,height,width) // For Set the Frame
sectionView.backgroundColor = UIColor.redColor()
return sectionView
}
像上面你可以設置部分的頁腳和 您可以添加標題的大小和頁腳
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 20.0
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 20.0
}
+0
我創建了一個包含imageView的cstom視圖。當我嘗試使用這種方法與一個簡單的視圖wotha nackground顏色它的作品,但不是與我的自定義視圖 – Byte
14
其實答案很簡單。 在viewForHeaderInSection()
中創建單元格對象,如您在cellForRowAtIndexPath()
中所做的那樣並將其返回。
以下是示例代碼。
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier") as! YourTableViewCell
return cell
}
您可以爲標題的設置高度:
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 80
}
相關問題
- 1. 頁眉和頁腳的自定義
- 2. TCPDF自定義頁眉和頁腳WriteHTML
- 3. 獲取UITableView頁眉/頁腳
- 4. UITableView自定義頁腳
- 5. 如何在Rotativa的頁眉或頁腳上設置圖像PDF
- 6. 如何在IBM Cognos中將頁腳/頁眉分割爲多個單元格?
- 7. 使用Swift自定義表格視圖的頁眉和頁腳
- 8. 如何將頁眉設置爲UICollectionView
- 9. 自定義頁眉
- 10. 設置自定義頁眉使用單義性
- 11. 將基本頁腳設置爲UITableView
- 12. 如何在jqgrid中自定義頁眉和頁腳?
- 13. 如何自定義tcpdf頁眉和頁腳?
- 14. 如何在laravel tcpdf中添加自定義頁眉和頁腳?
- 15. 自定義uitableview單元格?
- 16. UITableView自定義單元格
- 17. 動畫UITableView的頁眉和頁腳
- 18. 如何訪問UITableView中特定部分的頁腳或頁眉視圖?
- 19. PHP包含頁眉或頁腳位置
- 20. html頁面中的自定義頁眉和頁腳
- 21. 在VerticalFieldManager中設置頁眉和頁腳
- 22. Magento中PDP頁面的自定義模板或主題(頁眉和頁腳)
- 23. UITableView頁眉/頁腳字體顏色
- 24. 我們如何將頁眉,頁腳設置爲黑莓中的彈出屏幕?
- 25. 創建自定義HTML頁面頁眉和頁腳
- 26. 如何使用jsPDF-AutoTable插件在PDF中自定義頁眉單元格?
- 27. 如何在詩詞(XWPF)中設置頁眉和頁腳位置?
- 28. 自定義頁眉與$ _ SERVER
- 29. 自定義頁眉角JS
- 30. 自定義頁眉在WCF
不能使用的UITableView細胞作爲頁眉或頁腳,對於您需要創建一個視圖(廈門國際銀行),你可以用它 –
細胞作爲標題? tableHeaderView是一種視圖,而不是單元格 – ronan
如何使用和Xib創建UiView?當我創建一個新的CocoaTouch文件,並使其UIView的子類的「也創建Xib文件」的選項不能檢查 – Byte