☆ 最近發現 GitHub 實在太強大了,真的是一個能取代 Blog 的服務,未來大概只會更新到 GitHub 上。
https://github.com/kelp404

2012-08-18

UIActionSheet showFromRect with UITableViewCell

The UI of an UIActionSheet in an iPad is like this. It is different between an iPad and an iPhone.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"click?"
                                                             delegate:self
                                                    cancelButtonTitle:@"Cancel"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"do something", nil];
    [actionSheet showInView:[UIApplication sharedApplication].keyWindow];
   
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

I think the UIActionSheet is too far with the cell which was clicked. Cocoa provides [UIActionSheet showFromRect:...] let UIActionSheet pop from a rect.
CGRect cellRect = [tableView cellForRowAtIndexPath:indexPath].frame;
cellRect.origin.y += cellRect.size.height * 0.5;
cellRect.origin.y -= tableView.contentOffset.y;

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"click?"
                                                         delegate:self
                                                cancelButtonTitle:@"Cancel"
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:@"do something", nil];
[actionSheet showFromRect:cellRect inView:[UIApplication sharedApplication].keyWindow animated:YES];

Or let arrow of an actionsheet in the cell.
CGRect cellRect = [tableView cellForRowAtIndexPath:indexPath].frame;
cellRect.origin.y += cellRect.size.height;
cellRect.origin.y -= tableView.contentOffset.y;
cellRect.size.height = 1;

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"click?"
                                                         delegate:self
                                                cancelButtonTitle:@"Cancel"
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:@"do something", nil];
[actionSheet showFromRect:cellRect inView:[UIApplication sharedApplication].keyWindow animated:YES];

1 comment:

  1. Thanks a lot. This tutorial really helped me.

    ReplyDelete