开发者

a problem when disable Show selection on touch of UITableview?

开发者 https://www.devze.com 2022-12-18 09:16 出处:网络
I use IB and uncheck the \'Show selection on touch\' but it still show blue highlight on cell that is selected. Is thi开发者_如何学编程s a bug with apple or I am getting something wrong.This is probab

I use IB and uncheck the 'Show selection on touch' but it still show blue highlight on cell that is selected. Is thi开发者_如何学编程s a bug with apple or I am getting something wrong.


This is probably a bug in IB as you see in Documentation that table view does not have any property for the shows Selection on touch. It is the property of tableview cell rather. So the checkbox should not be present in the IB. Probably you can file a bug with apple and see what they say about it.

For getting the effect you should do it like:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

            cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
                [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            }
    }

Hope this helps.


I too feel it's most likely a bug. However, I just chanced upon a perfect workaround based on the following observations.

  • The cell is highlighted on touch down and selected on touch up.
  • Both -setHighlighted:animated: and -setSelected:animated: highlight the cell according to its selection style, i.e., turning either of them on while the other is off highlights the cell.
  • The cell is born with highlighted turned off (typically, else the solution below just needs an appropriate tweak which is easy to figure out based on your specific situation).

Given the above, just subclass UITableViewCell and override setHighlighted:animated: without calling super's implementation. Thus, all efforts to turn highlighted on would be suppressed and the highlighting would happen only on touch up and not on touch down which is exactly what is expected when turning off 'Show Selection on Touch'.

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    // hack to get the effect of unchecking "Show Selection on Touch" option of UITableView in IB which has no effect
}

On touch up, the cell is selected but it is not animated. If you want the animation, I found that calling a delegate method as shown below animates the selection.

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_itemsTable selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
    return indexPath;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消