0

From this post they said that it is kind of a bug in ios 7 and 8 - Button in UITableViewCell does not change to highlighted when tapped. Here I post one answer for this in Objective-C:

Create a custom UITableView subclass and custom UITableViewCell subclass.

Use this sample UITableView's initWithFrame:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self)
    {
        // iterate over all the UITableView's subviews
        for (id view in self.subviews)
        {
            // looking for a UITableViewWrapperView
            if ([NSStringFromClass([view class]) isEqualToString:@"UITableViewWrapperView"])
            {
                // this test is necessary for safety and because a "UITableViewWrapperView" is NOT a UIScrollView in iOS7
                if([view isKindOfClass:[UIScrollView class]])
                {
                    // turn OFF delaysContentTouches in the hidden subview
                    UIScrollView *scroll = (UIScrollView *) view;
                    scroll.delaysContentTouches = NO;
                }
                break;
            }
        }
    }
    return self;
}

Use this sample UITableViewCell's initWithStyle:reuseIdentifier:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self)
    {
        // iterate over all the UITableViewCell's subviews
        for (id view in self.subviews)
        {
            // looking for a UITableViewCellScrollView
            if ([NSStringFromClass([view class]) isEqualToString:@"UITableViewCellScrollView"])
            {
                // this test is here for safety only, also there is no UITableViewCellScrollView in iOS8
                if([view isKindOfClass:[UIScrollView class]])
                {
                    // turn OFF delaysContentTouches in the hidden subview
                    UIScrollView *scroll = (UIScrollView *) view;
                    scroll.delaysContentTouches = NO;
                }
                break;
            }
        }
    }

    return self;
}

However I cannot write in Swift. There are some problem:

1) I cannot do

self = super.init(style: style, reuseIdentifier: reuseIdentifier) 

Error: Cannot assign to 'self' in a method

2) In Swift, I cannot do

view.class

as in Objective C:

[view class]

I have searched for hours but still cannot get what I want.

Please anyone can answer this in Swift?

Community
  • 1
  • 1
thomasdao
  • 972
  • 12
  • 26

2 Answers2

1

In Swift, to call the superclass's designated initialiser, instead of calling self = [super initWithStyle:style reuseIdentifier:reuseIdentifier], you just use super.init(style: style, reuseIdentifier: reuseIdentifier)

Schemetrical
  • 5,506
  • 2
  • 26
  • 43
  • Thanks a lot. How about [NSStringFromClass([view class]) isEqualToString:@"UITableViewCellScrollView"]? I cannot use view.isKindOfClass(UITableViewCellScrollView). Error: Class not exists.. – thomasdao May 11 '15 at 13:07
  • So please tell me how to do [NSStringFromClass([view class]) isEqualToString:@"UITableViewCellScrollView"]. Would be much appreciated! :) – thomasdao May 11 '15 at 13:11
  • I'm a little confused here. The above code will work for ios 7 and 8, except that it is in objective c and I want to write in Swift. I think there will be solution for this. – thomasdao May 11 '15 at 13:22
  • try `NSStringFromClass(view.classForCoder) == "UITableViewCellScrollView"`. I was confused as `UITableViewCellScrollView` is a private class. – Schemetrical May 11 '15 at 13:25
  • I think the above condition will always return true – thomasdao May 11 '15 at 13:27
1

Thanks to Schemetrical, this is the working version for me. (iOS 7 + 8)

First I wrote a utility function:

class func classNameAsString(obj: AnyObject) -> String {
    return _stdlib_getDemangledTypeName(obj).componentsSeparatedByString(".").last!
}

then I subclass UITableView and implement this:

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    for view in self.subviews {
        if (Utility.classNameAsString(view) == "UITableViewWrapperView") {
            if view.isKindOfClass(UIScrollView) {
                var scroll = (view as UIScrollView)
                scroll.delaysContentTouches = false
            }
            break
        }
    }
}

I also subclass UITableViewCell and implement this:

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    for view in self.subviews {
        if (Utility.classNameAsString(view) == "UITableViewCellScrollView") {
            if view.isKindOfClass(UIScrollView) {
                var scroll = (view as UIScrollView)
                scroll.delaysContentTouches = false
            }

        }
    }
}
thomasdao
  • 972
  • 12
  • 26