概述
iOS prior to 6.0(included) is not considered. A respondsToSelector check might be needed in terms of compatibility, or exceptions like
“unrecognized selector sent to instance
” will occur.
1. eliminate extra separators
tableView.tableFooterView = [UIView new];
2. eliminate separator insets (iOS8 Only)
Setting the separatorInset of tableView to UIEdgeInsetsZero is not going to work. It will only effect the extra separators, not the cells that you created.
iOS8 introduces layoutMargins property on UIView, along with another property called preservesSuperviewLayoutMargins indicating whether superview’s layoutMargins is preserved (Think of it as an override) which is YES by default. layoutMargins property is a set of insets from the edge of the view’s bounds that denote a default spacing for laying out content.
The default separatorInset of tableView and tableViewCell is (top = 0, left = 15, bottom = 0, right = 0)
The default layoutMargins of tableView and tableViewCell is(top = 8, left = 8, bottom = 8, right = 8)
These 3 properties are defining the behaviour of separator indentation.
You have to set separatorInset and layoutMargins oftableViewCell directly:
tableViewCell.preservesSuperviewLayoutMargins = NO;
tableViewCell.separatorInset = UIEdgeInsetsZero;
tableViewCell.layoutMargins = UIEdgeInsetsZero;
You can put those code in cell’s init method or awakeFromNib or tableView’s delegate tableView:willDisplayCell:fromRowAtIndexPath, etc.
Don’t ask me why. I haven’t figured out myself.
http://stackoverflow.com/questions/25770119/ios-8-uitableview-separator-inset-0-not-working
3. UIButton-touch-delay in tableViewCell
When UIButton is added to UITableViewCell as a subview, the highlight state is only activated if you held your finger on the button for more than some fraction of a second. Table view cells are displayed using a UITableView which is a subclass of UIScrollView and as such inherits the ability to scroll. A scroll view intercepts touch events and has a short delay to check if the touch event is part of a scroll gesture. If it isn’t then the touch event is passed onto the corresponding view and the scroll will not be triggered. That explains the delay in the button highlight state.
We can remove the delay by setting UIScrollView.delaysContentTouches = NO. A side effect of this change is that scroll gesture cannot start from the button. Depending on how big your buttons are this might not be ideal.The work around for this is to override the (BOOL)touchesShouldCancelInContentView:(UIView *)view method in your UITableView subclass like so:
- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
// Because we set delaysContentTouches = NO, we return YES for UIButtons
// so that scrolling works correctly when the scroll gesture
// starts in the UIButtons.
if ([view isKindOfClass:[UIButton class]]) {
return YES;
}
return [super touchesShouldCancelInContentView:view];
}
However, this is not enough for iOS 8 as the UITableViewCell has a build-in subview called UITableViewCellScrollView which is a subclass of UIScrollView. That means three are more than one UIScrollView in the button’s superviews. So besides setting UITableView’s delayContentTouches, you have to set UITableViewCell too.
for (id currentView in tableViewCell.subviews) {
if ([NSStringFromClass([currentView class]) isEqualToString:@"UITableViewCellScrollView"]) {
if ([currentView isKindOfClass:[UIScrollView class]]) {
[(UIScrollView *)currentView setDelaysContentTouches:NO];
}
}
}
http://charlesharley.com/2013/programming/uibutton-in-uitableviewcell-has-no-highlight-state/
4. UITableViewCell changes all its subviews’ background color when selected
The problem is like this: Put a UIButton onto UITableViewCell as a subview of UITableViewCell.contentView first. And when the cell is selected the button’s background becomes transparent. This feature that UITableViewCell changes the backgroundColor of all subviews on selection to clearColor is weird. What the system does under the hood is:
Set all its subviews' backgroundColor to clear color(transparent).
Highlight all subviews that can be highlighted(UIControl excluded), for example, UIImageView.
If it’s UIButton that you really care about, the solution is simple: Set UIButton’s backgroundImage of normal state instead of backgroundColor. If not, the solution is may not be that comfortable. There are still 3 options:
1. If your view’s backgroundColor has no change to be clearColor, you can subclass the view and override setBackgroundColor: and check if the parameter is clearColor, and do nothing if YES.
2. You can override setBackgroundColor and do nothing in it. And add your own property, say MyOwnBackgroundColor, and use it to set the backgroundColor (call [super setBackgroundColor:]).
3. override these two methods in your UITableViewCell subclass:
- (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.
}
}
https://github.com/DylanVann/DVColorLockView
http://stackoverflow.com/questions/14468449/the-selectedbackgroundview-modifies-the-contentview-subviews
龙西村原创, 转载请注明出处.
最后
以上就是酷酷紫菜为你收集整理的UITableView那些坑的全部内容,希望文章能够帮你解决UITableView那些坑所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复