iPhone的UITableView – 删除button

我正在使用UITableView的“轻扫删除”function。

问题是我正在使用每个项目的基础上创build一个自定义的UITableViewCell

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

我需要改变删除button的位置(简单地把它移动到左边10px左右),我将如何去做这个?

这是我创build单元格的现有代码:

 - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"cellForRowAtIndexPath"); #if USE_CUSTOM_DRAWING const NSInteger TOP_LABEL_TAG = 1001; const NSInteger BOTTOM_LABEL_TAG = 1002; UILabel *topLabel; UILabel *bottomLabel; #endif static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { // // Create the cell. // cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; #if USE_CUSTOM_DRAWING const CGFloat LABEL_HEIGHT = 20; UIImage *image = [UIImage imageNamed:@"trans_clock.png"]; // // Create the label for the top row of text // topLabel = [[[UILabel alloc] initWithFrame: CGRectMake( image.size.width + 2.0 * cell.indentationWidth, 0.5 * (aTableView.rowHeight - 2 * LABEL_HEIGHT), aTableView.bounds.size.width - image.size.width - 4.0 * cell.indentationWidth , LABEL_HEIGHT)] autorelease]; [cell.contentView addSubview:topLabel]; // // Configure the properties for the text that are the same on every row // topLabel.tag = TOP_LABEL_TAG; topLabel.backgroundColor = [UIColor clearColor]; topLabel.textColor = fontColor; topLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0]; topLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]]; // // Create the label for the top row of text // bottomLabel = [[[UILabel alloc] initWithFrame: CGRectMake( image.size.width + 2.0 * cell.indentationWidth, 0.5 * (aTableView.rowHeight - 2 * LABEL_HEIGHT) + LABEL_HEIGHT, aTableView.bounds.size.width - image.size.width - 4.0 * cell.indentationWidth , LABEL_HEIGHT)] autorelease]; [cell.contentView addSubview:bottomLabel]; // // Configure the properties for the text that are the same on every row // bottomLabel.tag = BOTTOM_LABEL_TAG; bottomLabel.backgroundColor = [UIColor clearColor]; bottomLabel.textColor = fontColor; bottomLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0]; bottomLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize] - 2]; // // Create a background image view. // cell.backgroundView = [[[UIImageView alloc] init] autorelease]; cell.selectedBackgroundView = [[[UIImageView alloc] init] autorelease]; #endif } #if USE_CUSTOM_DRAWING else { topLabel = (UILabel *)[cell viewWithTag:TOP_LABEL_TAG]; bottomLabel = (UILabel *)[cell viewWithTag:BOTTOM_LABEL_TAG]; } topLabel.text = @"Example Text"; topLabel.textColor = fontColor; bottomLabel.text = @"More Example Text"; bottomLabel.textColor = fontColor; // // Set the background and selected background images for the text. // Since we will round the corners at the top and bottom of sections, we // need to conditionally choose the images based on the row index and the // number of rows in the section. // UIImage *rowBackground; UIImage *selectionBackground; NSInteger sectionRows = [aTableView numberOfRowsInSection:[indexPath section]]; NSInteger row = [indexPath row]; if (row == 0 && row == sectionRows - 1) { rowBackground = [UIImage imageNamed:@"topAndBottomRow.png"]; selectionBackground = [UIImage imageNamed:@"topAndBottomRowSelected.png"]; } else if (row == 0) { rowBackground = [UIImage imageNamed:@"topRow.png"]; selectionBackground = [UIImage imageNamed:@"topRowSelected.png"]; } else if (row == sectionRows - 1) { rowBackground = [UIImage imageNamed:@"bottomRow.png"]; selectionBackground = [UIImage imageNamed:@"bottomRowSelected.png"]; } else { rowBackground = [UIImage imageNamed:@"middleRow.png"]; selectionBackground = [UIImage imageNamed:@"middleRowSelected.png"]; } ((UIImageView *)cell.backgroundView).image = rowBackground; ((UIImageView *)cell.selectedBackgroundView).image = selectionBackground; cell.imageView.image = [UIImage imageNamed:@"Example_Image.png"]; #else cell.text = @"Example"; #endif return cell; } 

Iwat的代码不适合我。 但是这个工作。

  - (void)willTransitionToState:(UITableViewCellStateMask)state {

     [super willTransitionToState:state];

     if((state&UITableViewCellStateShowingDeleteConfirmationMask)== UITableViewCellStateShowingDeleteConfirmationMask){

         for(UIView * subview in self.subviews){

             if([NSStringFromClass([subview class])isEqualToString:@“UITableViewCellDeleteConfirmationControl”]){             

                 subview.hidden = YES;
                 subview.alpha = 0.0;
             }
         }
     }
 }

 - (void)didTransitionToState:(UITableViewCellStateMask)state {

     [super didTransitionToState:state];

     if( state == UITableViewCellStateShowingDeleteConfirmationMask || state == UITableViewCellStateDefaultMask ){
         for(UIView * subview in self.subviews){

             if([NSStringFromClass([subview class])isEqualToString:@“UITableViewCellDeleteConfirmationControl”]){

                 UIView * deleteButtonView =(UIView *)[subview.subviews objectAtIndex:0]; 
                  CGRect f = deleteButtonView.frame; 
                  f.origin.x  -  = 20; 
                  deleteButtonView.frame = f;

                 subview.hidden = NO;

                 [UIView beginAnimations:@“anim”context:nil];
                 subview.alpha = 1.0;
                 [UIView commitAnimations];
             }
         }
     }
 } 

对我来说,解决这个问题的最好方法是在MyCell:UITableViewCell覆盖-(void)layoutSubviews MyCell:UITableViewCell

在这里,您不仅可以看到“删除”button的自定义位置,还可以重新定位“ 编辑”模式的“编辑”和“重新sorting”控件

 - (void)layoutSubviews { [super layoutSubviews]; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:0.0f]; for (UIView *subview in self.subviews) { if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) { CGRect newFrame = subview.frame; newFrame.origin.x = 200; subview.frame = newFrame; } else if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellEditControl"]) { CGRect newFrame = subview.frame; newFrame.origin.x = 100; subview.frame = newFrame; } else if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellReorderControl"]) { CGRect newFrame = subview.frame; newFrame.origin.x = 200; subview.frame = newFrame; } } [UIView commitAnimations]; } 

我还没有能够改变删除button的实际外观,但你可以改变文字。 也许你可以用它来获得你正在寻找的效果?

查看UITableViewDelegate协议的以下成员:

 - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath 

此外,通过UITableViewCell并覆盖以下内容,您可以检测何时显示删除button:

 - (void)willTransitionToState:(UITableViewCellStateMask)state - (void)didTransitionToState:(UITableViewCellStateMask)state 

对于其中的任何一个,当显示删除button时,状态掩码将是UITableViewCellStateShowingDeleteConfirmationMask

希望这些线索将指向你正确的方向。

我使用的是一见钟情的解决scheme,但是却没有依赖无证apis:

 /** * Transition to state */ -(void)willTransitionToState:(UITableViewCellStateMask)state { if(state & UITableViewCellStateShowingDeleteConfirmationMask) _deleting = YES; else if(!(state & UITableViewCellStateShowingDeleteConfirmationMask)) _deleting = NO; [super willTransitionToState:state]; } /** * Reset cell transformations */ -(void)resetCellTransform { self.transform = CGAffineTransformIdentity; _background.transform = CGAffineTransformIdentity; _content.transform = CGAffineTransformIdentity; } /** * Move cell around if we are currently in delete mode */ -(void)updateWithCurrentState { if(_deleting) { float x = -20; float y = 0; self.transform = CGAffineTransformMakeTranslation(x, y); _background.transform = CGAffineTransformMakeTranslation(-x, -y); _content.transform = CGAffineTransformMakeTranslation(-x, -y); } } -(void)setFrame:(CGRect)frame { [self resetCellTransform]; [super setFrame:frame]; [self updateWithCurrentState]; } -(void)layoutSubviews { [self resetCellTransform]; [super layoutSubviews]; [self updateWithCurrentState]; } 

基本上这改变了细胞的位置,并重新调整可见部分。 WillTransitionToState只是设置一个实例variables,指示单元格是否处于删除模式。 重写setFrame是必要的,以支持旋转手机风景,反之亦然。 其中_background是我的单元格的背景视图,_content是添加到单元格的contentView的视图,包含所有标签等

我不知道有什么办法“将删除button10px移到左边”。 但是,通过侦听来自UITableViewCell子类的willTransitionToState:消息,您可以在不可移动的删除button的静态位置周围设置表格单元格的自定义内容。

引用文档:

UITableViewCell的子类可以实现此方法,以便在更改状态时为单元格附加更改。 每当一个单元格在状态之间转换时,UITableViewCell就会调用这个方法,比如从正常状态(默认)到编辑模式。 自定义单元格可以设置和定位以新状态出现的任何新视图。 然后,单元接收一个layoutSubviews消息(UIView),在这个消息中它可以将这些新的视图定位在新状态的最终位置。 重写此方法时子类必须始终调用super。

你正在寻找的是UITableViewCellStateShowingDeleteConfirmationMask值。 这是创buildUITableViewCell子类可能更好的时代之一,所以从控制器,你只是创build自定义单元格的实例。 然后单元格可以像animateLeftanimateRight一样调整自己,并处理调整自己的子视图的内部工作。

我不知道最终的解决scheme,但据我所知,下面的代码可能是有用的。

 // subclass UITableViewCell - (void)willTransitionToState:(UITableViewCellStateMask)state { [super willTransitionToState:state]; if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableCellStateShowingDeleteConfirmationMask) { for (UIView *subview in self.subviews) { if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) { subview.hidden = YES; subview.alpha = 0; } } } } - (void)didTransitionToState:(UITableViewCellStateMask)state { [super willTransitionToState:state]; if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableCellStateShowingDeleteConfirmationMask) { for (UIView *subview in self.subviews) { if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) { subview.frame = CGRectMake(subview.frame.origin.x - 10, subview.frame.origin.y, subview.frame.size.width, subview.frame.size.height); subview.hidden = NO; [UIView beginAnimations:@"anim" context:nil]; subview.alpha = 1; [UIView commitAnimations]; } } } }