2015-03-26 17 views
11

Tôi muốn vuốt ô khi nhấp vào nút. Tôi thành công khi vuốt một ô. Nhưng tôi muốn swipe vào nút đó là trong tế bào. mã của tôi làCách vuốt ô khi nhấp vào nút

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleTableIdentifier = @"SimpleCell"; 
    SimpleCell *cell = (SimpleCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
    if (cell == nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 
    SimpleCell *cekks=[[SimpleCell alloc]init]; 
    cekks.scrollButton.alpha =0.0; 

    NSString *titleString; 
    UIButton *sender = [[UIButton alloc]init]; 
    //[sender setBackgroundImage:[UIImage imageNamed:@"swipe.png"] forState:UIControlStateNormal]; 
    sender.tag = indexPath.row; 

    titleString [email protected]"Send A Gift"; 
    UITableViewRowAction *sendAGift = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:titleString handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){ 
     // [self deleteMail:[NSArray arrayWithObject:indexPath]:YES]; 

    }]; 

    [sendAGift setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"swipe.png"]]]; 



    return @[sendAGift]; 
} 
+0

bạn muốn gì? Swipe on Button click hoặc Swipe trên nút mà làm cho các tế bào swipe –

+0

Khi nhấp vào một nút trên tế bào Swipe –

+0

Swipe các tế bào khi bạn nhấp vào nút, phải không? –

Trả lời

3

Tôi nghĩ rằng lớp học của bạn SimpleCell nên chứa UIButton, để thực hiện tái sử dụng một cách chính xác. Nó sẽ đơn giản hơn nếu bạn tạo một tùy chỉnh UITableViewCell có chứa tất cả các hành động UI trên ô và vận hành chúng trong ô, chứ không phải trong chính bản thân số UITableView.

Chúng ta hãy xem một ví dụ:

Dưới đây là file customCell.h:

@interface customCell : UITableViewCell { 
    UIButton *buttonSwipe; 
} 
@end 

Dưới đây là file customCell.h:

#import "customCell.h" 

@implementation customCell 

- (instancetype) initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     [self commonInit]; 
    } 

    return self; 
} 

- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     [self commonInit]; 
    } 

    return self; 
} 

- (instancetype)initWithCoder:(NSCoder *)aDecoder { 
    self = [super initWithCoder:aDecoder]; 
    if (self) { 
     [self commonInit]; 
    } 

    return self; 
} 

- (void) commonInit { 
    buttonSwipe = [UIButton... // Initialize here your button 
    [buttonSwipe addTarget:self action:@selector(swipeCell:) forControlEvents:UIControlEventTouchUpInside]; 
} 

- (void) swipeCell { 
    // Embed here the code that makes the effect of swipe the cell. 
} 

Đây là giải pháp nhanh chưa được kiểm tra với một số mã được xác định trước, nhưng tôi nghĩ đó là ví dụ hoạt động nếu bạn muốn làm cho các ô của mình vuốt bằng mã của riêng bạn.

Nhưng nếu bạn muốn có một cách nhanh hơn, tôi khuyên bạn nên đến thăm Chris WendelSWTableViewCell mình trên GitHub

+0

trong phương pháp swipecell cách chúng tôi gọi phương thức đại biểu của bảng ... Không có i m không cần thiết này –

+0

Mmmh Tôi không thể thấy lý do tại sao bạn muốn gọi phương thức ủy nhiệm UITableView từ bên trong một ô ... Bạn có thể giải thích nó không? –

+0

trong iOS 8 trên swiping một tế bào có phương pháp - (BOOL) tableView: (UITableView *) tableView canEditRowAtIndexPath: (NSIndexPath *) indexPath - (void) tableView: (UITableView *) tableView commitEditingStyle: (UITableViewCellEditingStyle) editStyle forRowAtIndexPath: (NSIndexPath *) indexPath –

1

Bạn có thể gọi editActionsForRowAtIndexPath: Phương pháp như

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:SELECTED_ROW_INDEX inSection:0]; 
    [self tableView:self.tableView editActionsForRowAtIndexPath:indexPath]; 

thử đoạn code dưới đây

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 


    //swipe button allocation 
      UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)]; 
      btn.tag = indexPath.row; 
      [cell.contentView addSubview:btn]; 
      [btn addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside]; 
      cell.textLabel.text = [NSString stringWithFormat:@"%lu",indexPath.row]; 
      return cell; 
     } 
     -(void)buttonTouched:(id)sender{ 

      UIButton *btn = (UIButton *)sender; 
      NSIndexPath *indexPath = [NSIndexPath indexPathForRow:btn.tag inSection:0]; 
      [self tableView:self.tableView editActionsForRowAtIndexPath:indexPath]; 
     } 
     - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
      // Return NO if you do not want the specified item to be editable. 
      return YES; 
     } 

     - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
      if (editingStyle == UITableViewCellEditingStyleDelete) { 
       [self.objects removeObjectAtIndex:indexPath.row]; 
       [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      } else if (editingStyle == UITableViewCellEditingStyleInsert) { 
       // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. 
      } 
     } 

     -(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{ 
      NSLog(@"edit"); 
      // code 
      return nil; 
     } 
+0

editActionsForRowAtIndexPath cuộc gọi nhưng không hiển thị các nút tùy chọn –

+0

tôi đoán có điều gì đó bị thiếu –

Các vấn đề liên quan