2015-03-26 14 views
11

Chcę przesunąć komórkę po kliknięciu przycisku. Udało mi się przesuwać komórkę. Ale chcę przesunąć przycisk, który znajduje się w komórce. mój kod jestSposób przesuwania komórki po kliknięciu przycisku

- (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

czego chcesz? Przesuń palcem po przycisku kliknij lub przesuń palcem po przycisku, który powoduje przesunięcie komórki –

+0

Kliknięcie przycisku na komórce Przesuń palcem –

+0

Przesuń palcem komórkę po kliknięciu przycisku, prawda? –

Odpowiedz

3

myślę klasa SimpleCell powinien zawierać UIButton, w celu przeprowadzenia ponownego użycia poprawnie.

Będzie łatwiejsze, jeśli utworzysz niestandardową UITableViewCell, która zawiera wszystkie działania interfejsu użytkownika nad komórką i będzie obsługiwać je w komórce, a nie w samym UITableView.

Zobaczmy przykład:

Oto plik customCell.h:

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

Oto plik 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. 
} 

Jest to szybko niesprawdzone obejście z niektórymi predefiniowany kod, ale myślę, że to działający przykład, jeśli chcesz, aby twoje komórki przesuwały z własnym kodem.

Ale jeśli chcesz mieć szybszy sposób, polecam do odwiedzenia Chris Wendel i jego SWTableViewCell na GitHub

+0

w swipecell metoda, jak nazywamy delegate metoda tabeli ... Nie jestem potrzebna to –

+0

Mmmh Nie widzę powodu, dlaczego chcesz wywołać metody delegatów UITableView z wnętrza komórki ... Czy możesz wyjaśnić to? –

+0

w iOS 8 przy przesuwaniu komórki tam jest metoda - (BOOL) tableView: (UITableView *) tableView canEditRowAtIndexPath: (NSIndexPath *) indexPath - (void) tableView: (UITableView *) tableView commitEditingStyle: (UITableViewCellEditingStyle) editingStyle forRowAtIndexPath: (NSIndexPath *) indexPath –

1

Można zadzwonić editActionsForRowAtIndexPath: Sposób

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

wypróbować poniższy kod

- (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 wywołania, ale nie pokazano przyciski opcji –

+0

Domyślam się, że czegoś brakuje –

Powiązane problemy