iOS的UITableView常见问题总结

1、如何设置headerView以及其高度

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
tableView.tableHeaderView = myHeaderView
let height = headerView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
var frame = headerView.frame
frame.size.height = height
headerView.frame = frame
tableView.tableHeaderView = myHeaderView let height = headerView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height var frame = headerView.frame frame.size.height = height headerView.frame = frame
tableView.tableHeaderView = myHeaderView

let height = headerView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
var frame = headerView.frame
frame.size.height = height
headerView.frame = frame

2、去掉多余cell的分割线

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

3、如何设置section数、行数

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
extension MyViewController: UITableViewDataSource {
// section数
func numberOfSections(in: UITableView) -> Int {
}
// row数
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
}
// 在section和row下,cell
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
}
}
extension MyViewController: UITableViewDataSource { // section数 func numberOfSections(in: UITableView) -> Int { } // row数 public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { } // 在section和row下,cell public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { } }
extension MyViewController: UITableViewDataSource {

    // section数
    func numberOfSections(in: UITableView) -> Int {
    }

    // row数
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    }

    //  在section和row下,cell
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    }

}

4、iOS 8+自动计算行高、section高度

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
tableView.estimatedRowHeight = 80
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 80 tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 80
tableView.rowHeight = UITableViewAutomaticDimension

实际上,sectionHeader高度也可以自动算高

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
tv.estimatedSectionHeaderHeight = 20
tv.sectionHeaderHeight = UITableViewAutomaticDimension
tv.estimatedSectionHeaderHeight = 20 tv.sectionHeaderHeight = UITableViewAutomaticDimension
tv.estimatedSectionHeaderHeight = 20
tv.sectionHeaderHeight = UITableViewAutomaticDimension

当然sectionFooter也可以,不再赘述

5、禁用tableview自带的分割线

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
tv.separatorStyle = .none
tv.separatorStyle = .none
tv.separatorStyle = .none

6、设置sectionHeader和sectionFooter,以及他们的高度

view

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
extension MyViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
}
}
extension MyViewController: UITableViewDelegate { func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { } func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { } }
extension MyViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    }

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

    }
}

高度

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
extension TTEntranceExamReportViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
}
}
extension TTEntranceExamReportViewController: UITableViewDelegate { func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { } func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { } }
extension TTEntranceExamReportViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    }
}

7、点击cell有阴影,抬起时候阴影消失

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: no)
// other code
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: no) // other code }
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: no)
    // other code
}

8、iPad的UITableViewCell自动缩进的问题

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
if (IS_IPAD && [_tableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)]) {
_tableView.cellLayoutMarginsFollowReadableWidth = NO;
}
if (IS_IPAD && [_tableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)]) { _tableView.cellLayoutMarginsFollowReadableWidth = NO; }
if (IS_IPAD && [_tableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)]) {
    _tableView.cellLayoutMarginsFollowReadableWidth = NO;
}

Swift版:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
if IS_IPAD, #available(iOS 9.0, *) {
tableView.cellLayoutMarginsFollowReadableWidth = false
}
if IS_IPAD, #available(iOS 9.0, *) { tableView.cellLayoutMarginsFollowReadableWidth = false }
if IS_IPAD, #available(iOS 9.0, *) {
    tableView.cellLayoutMarginsFollowReadableWidth = false
}

9、设定UITableviewCell按下的点击效果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cell.selectedBackgroundView = [[PureColorView alloc] initWithColor:[UIColor redColor]];
cell.selectedBackgroundView = [[PureColorView alloc] initWithColor:[UIColor redColor]];
cell.selectedBackgroundView = [[PureColorView alloc] initWithColor:[UIColor redColor]];

PureColorView是将颜色转化为纯色View的类,网上可以搜到

10、sectionHeader不吸顶

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let tv = UITableView(frame: CGRect.zero, style: .grouped)
let tv = UITableView(frame: CGRect.zero, style: .grouped)
let tv = UITableView(frame: CGRect.zero, style: .grouped)

11、使用.grouped后,TableView底部有20px多余空白

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
tv.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: CGFloat.leastNormalMagnitude))
tv.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: CGFloat.leastNormalMagnitude))
tv.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: CGFloat.leastNormalMagnitude))

12、ios 8系统上,点击cell push一个vc,再pop回来,部分cell高度会乱掉

需要强制实现下估算高度

传送门

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return self.tableView(tableView, heightForRowAt: indexPath)
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { return self.tableView(tableView, heightForRowAt: indexPath) }
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return self.tableView(tableView, heightForRowAt: indexPath)
}

13、tableview向上错一点

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
_tableView.contentInset = UIEdgeInsetsMake(-TT_1PX, 0, 0, 0);
_tableView.contentInset = UIEdgeInsetsMake(-TT_1PX, 0, 0, 0);
_tableView.contentInset = UIEdgeInsetsMake(-TT_1PX, 0, 0, 0);

14、禁用按下弹不起来的效果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: false)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: false) }
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: false)
}

15、不要用CGRect.zero初始化tableview,否则可能viewDidLoad中初始化UI时会导致布局冲突,建议一开始就给一个大致的frame,后续在viewDidLoad中可以再次用autolayout约束tableview

16、使用.grouped后,section之间的间距变大

需要设置下section的header和footer高度:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
extension xxx: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.1
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0.1
}
}
extension xxx: UITableViewDelegate { func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { return 0.1 } func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 0.1 } }
extension xxx: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.1
    }
}

17、使用.grouped后,顶部高度变大

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
tv.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: CGFloat.leastNormalMagnitude))
tv.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: CGFloat.leastNormalMagnitude))
tv.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: CGFloat.leastNormalMagnitude))

18、右滑删除

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
extension XXXController: UITableViewDelegate {
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// do the delete
}
}
}
extension XXXController: UITableViewDelegate { func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { return true } func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { // do the delete } } }
extension XXXController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // do the delete
        }
    }
}

19、系统默认editMode的使用

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
tableview.allowsMultipleSelectionDuringEditing = true
tableView.isEditing = true
tableview.allowsMultipleSelectionDuringEditing = true tableView.isEditing = true
tableview.allowsMultipleSelectionDuringEditing = true
tableView.isEditing = true

这样搞完后,是用的系统默认的选项框,蓝色底圆形,白色对勾那种

想改颜色的话可以

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
tableView.tintColor = xxxx
tableView.tintColor = xxxx
tableView.tintColor = xxxx

20、更改cell按下时的颜色

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
selectedBackgroundView = TTPureColorView(color: UIColor.UI_pressBackgroundColor)
selectedBackgroundView = TTPureColorView(color: UIColor.UI_pressBackgroundColor)
selectedBackgroundView = TTPureColorView(color: UIColor.UI_pressBackgroundColor)

21、更改cell按下颜色的后遗症

如果按照上面设置,ios默认会将所有subview的背景色设为透明,以便展现出selectedbackgroundview。如果你的cell中有的view背景是彩色的,就会悲剧。

可以这么搞,复写如下方法:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
if(highlighted){
self.contentView.backgroundColor = colorYouWantWhenHighlighted;
}else{
self.contentView.backgroundColor = colorYouWantWhenUnhighlighted;
}
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated { if(highlighted){ self.contentView.backgroundColor = colorYouWantWhenHighlighted; }else{ self.contentView.backgroundColor = colorYouWantWhenUnhighlighted; } }
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    if(highlighted){
        self.contentView.backgroundColor = colorYouWantWhenHighlighted;
    }else{
        self.contentView.backgroundColor = colorYouWantWhenUnhighlighted;
    }
}

并且让:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cell.selectionStyle = UITableViewCellSelectionStyleNone
cell.selectionStyle = UITableViewCellSelectionStyleNone
cell.selectionStyle = UITableViewCellSelectionStyleNone

22、上面解法21也有个后遗症,那就是没法select cell了,下面这种解法 也算可以接受吧

不设置selectionStyle

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
// Recover backgroundColor of subviews.
}
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected) {
// Recover backgroundColor of subviews.
}
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated { [super setHighlighted:highlighted animated:animated]; if (highlighted) { // Recover backgroundColor of subviews. } } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; if (selected) { // Recover backgroundColor of subviews. } }
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];
    if (highlighted) {
        // Recover backgroundColor of subviews.
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    if (selected) {
        // Recover backgroundColor of subviews.
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *