iOS中UITableView使用的常見(jiàn)問(wèn)題總結(jié)
1、如何設(shè)置headerView以及其高度
tableView.tableHeaderView = myHeaderView let height = headerView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height var frame = headerView.frame frame.size.height = height headerView.frame = frame
2、去掉多余cell的分割線
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
3、如何設(shè)置section數(shù)、行數(shù)
extension MyViewController: UITableViewDataSource {
// section數(shù)
func numberOfSections(in: UITableView) -> Int {
}
// row數(shù)
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
}
// 在section和row下,cell
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
}
}
4、iOS 8+自動(dòng)計(jì)算行高、section高度
tableView.estimatedRowHeight = 80 tableView.rowHeight = UITableViewAutomaticDimension
實(shí)際上,sectionHeader高度也可以自動(dòng)算高
tv.estimatedSectionHeaderHeight = 20 tv.sectionHeaderHeight = UITableViewAutomaticDimension
當(dāng)然sectionFooter也可以,不再贅述
5、禁用tableview自帶的分割線
tv.separatorStyle = .none
6、設(shè)置sectionHeader和sectionFooter,以及他們的高度
view
extension MyViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
}
}
高度
extension TTEntranceExamReportViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
}
}
7、點(diǎn)擊cell有陰影,抬起時(shí)候陰影消失
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: no)
// other code
}
8、iPad的UITableViewCell自動(dòng)縮進(jìn)的問(wèn)題
if (IS_IPAD && [_tableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)]) {
_tableView.cellLayoutMarginsFollowReadableWidth = NO;
}
Swift版:
if IS_IPAD, #available(iOS 9.0, *) {
tableView.cellLayoutMarginsFollowReadableWidth = false
}
9、設(shè)定UITableviewCell按下的點(diǎn)擊效果
cell.selectedBackgroundView = [[PureColorView alloc] initWithColor:[UIColor redColor]];
PureColorView是將顏色轉(zhuǎn)化為純色View的類,網(wǎng)上可以搜到
10、sectionHeader不吸頂
let tv = UITableView(frame: CGRect.zero, style: .grouped)
11、使用.groupted后,TableView底部有20px多余空白
tv.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: CGFloat.leastNormalMagnitude))
12、ios 8系統(tǒng)上,點(diǎn)擊cell push一個(gè)vc,再pop回來(lái),部分cell高度會(huì)亂掉
需要強(qiáng)制實(shí)現(xiàn)下估算高度
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return self.tableView(tableView, heightForRowAt: indexPath)
}
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)各位iOS開發(fā)者們能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- ios UITableView實(shí)現(xiàn)無(wú)數(shù)據(jù)加載占位圖片
- iOS中UIScrollView嵌套UITableView的實(shí)踐教程
- IOS UITableView和NavigationBar的常用設(shè)置詳解
- iOS基于UITableView實(shí)現(xiàn)多層展開與收起
- iOS中UITableView Cell實(shí)現(xiàn)自定義單選功能
- iOS中的UITableView的重用機(jī)制與加載優(yōu)化詳解
- IOS UITableViewCell詳解及按鈕點(diǎn)擊事件處理實(shí)例
- IOS中UITableView滾動(dòng)到指定位置
- IOS UITableView顏色設(shè)置的實(shí)例詳解
相關(guān)文章
IOS設(shè)計(jì)模式之組合設(shè)計(jì)模式
組合模式,Composite Pattern,是一個(gè)非常巧妙的模式。幾乎所有的面向?qū)ο笙到y(tǒng)都應(yīng)用到了組合模式,接下來(lái)通過(guò)本文給大家介紹IOS設(shè)計(jì)模式之組合設(shè)計(jì)模式,需要的朋友參考下2016-02-02
iOS實(shí)現(xiàn)文件切片儲(chǔ)存并且上傳(仿斷點(diǎn)續(xù)傳機(jī)制)
這篇文章主要給大家介紹了關(guān)于iOS實(shí)現(xiàn)文件切片儲(chǔ)存并上傳仿斷點(diǎn)續(xù)傳機(jī)制的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
深入解析iOS應(yīng)用開發(fā)中九宮格視圖布局的相關(guān)計(jì)算方法
這篇文章主要介紹了iOS應(yīng)用開發(fā)中九宮格視圖布局的計(jì)算方法,包括item尺寸和坐標(biāo)等一系列影像布局的數(shù)值相關(guān)計(jì)算的講解,需要的朋友可以參考下2016-03-03
實(shí)例解析iOS開發(fā)中系統(tǒng)音效以及自定義音效的應(yīng)用
這篇文章主要介紹了iOS開發(fā)中系統(tǒng)音效以及自定義音效的應(yīng)用,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-10-10
IOS開發(fā)之JSON轉(zhuǎn)PLIST實(shí)例詳解
這篇文章主要介紹了IOS開發(fā)之JSON轉(zhuǎn)PLIST實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-11-11
iOS掃描二維碼實(shí)現(xiàn)手勢(shì)拉近拉遠(yuǎn)鏡頭
這篇文章主要為大家詳細(xì)介紹了iOS掃描二維碼實(shí)現(xiàn)手勢(shì)拉近拉遠(yuǎn)鏡頭,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
實(shí)例講解iOS中的CATransition轉(zhuǎn)場(chǎng)動(dòng)畫使用
CATransition類為應(yīng)用程序的轉(zhuǎn)場(chǎng)動(dòng)畫提供了很多可控制參數(shù),接下來(lái)我們就以幾個(gè)實(shí)例講解iOS中的CATransition轉(zhuǎn)場(chǎng)動(dòng)畫使用,需要的朋友可以參考下2016-06-06

