如何更改UITableViewCell的宽度?

问题描述:

我正在致力于UITableView,它有一个SwipeGesture。 在滑动,小区应该向左滑动,并且被设定在320应该出现如何更改UITableViewCell的宽度?

+0

尝试阅读本教程。 http://www.raywenderlich.com/62435/make-swipeable-table-view-cell-actions-without-going-nuts-scroll-views 这将清除你的所有疑虑和问题。 – Deepak 2014-09-24 06:35:34

它们放入UITableViewCellcontentView并就改变所述帧到x = -320,它会在contentView动画对象工作。

UITableView将单元格框架添加到表格视图时更改它们的框架。如果要更改单元格的宽度,则应该更改表格的宽度,或者更改单元格中contentView的宽度。

一种更好的方式来实现,这是继承UITableViewCell,并覆盖其-setFrame:方法是这样的:

试试这个:

- (void)setFrame:(CGRect)frame 
{ 
    frame.origin.x += inset; 
    frame.size.width -= 2 * inset; 
    [super setFrame:frame]; 
} 
+0

得到一个错误“**没有可见的UITableViewController接口声明选择器setFrame:**” – 2014-09-24 06:40:11

+0

否则你必须在单元格上添加一个视图,当你想左侧滑动时,你必须从右到左动画该视图 – 2014-09-24 06:41:46

+0

我试过但没有增益:( – 2014-09-24 06:45:15

尝试下面的代码。

// Create the swipe view 

    //Change the frame as per your requirement 

    UIView* _swipeView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, 10, 70)];  
    //setting the background 
    [_swipeView setBackgroundColor:[UIColor redColor]]; 

    [_swipeView setTag:1896]; 
    [cell.contentView addSubview:_swipeView];   

    // Create the gesture recognizers 
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeRightInCell:)]; 
    [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight]; 

    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeLeftInCell:)]; 
    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft]; 

    [cell addGestureRecognizer:swipeRight]; 
    [cell addGestureRecognizer:swipeLeft]; 

>

-(void) didSwipeRightInCell:(UISwipeGestureRecognizer*)ges{ 
     UITableViewCell* cellViewInProgress_ = (UITableViewCell*)ges.view; 

     for(UIView* view in cellViewInProgress.contentView.subviews) 
     { 
      if(view.tag == 1896){ 
       [UIView animateWithDuration:0.5 animations:^{ 

//Change the frame as per your requirement 
        view.frame = CGRectMake(5, 5, 220, 70); 
       }]; 

    // I have placed UILabel you can try your own control 

//Change the frame as per your requirement 

       UILabel *lblDemo = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 200, 30)]; 
       [lblDemo setFont:[Support getAppFontSemiBold:16.0]]; 
       [lblDemo setTextColor:[UIColor whiteColor]]; 
       [lblDemo setBackgroundColor:[UIColor clearColor]]; 
       lblDemo.tag = 2341; 
       [lblDemo setText:@"Hello"]]; 
       [view addSubview:lblDemo]; 

      } 
     } 
    } 

>

-(void) didSwipeLeftInCell :(UISwipeGestureRecognizer*)ges{ 
     UITableViewCell* cellViewInProgress_ = (UITableViewCell*)ges.view; 

     for(UIView* view in cellViewInProgress.contentView.subviews) 
     { 
      if(view.tag == 1896){ 
       UILabel* lbl = (UILabel*)[view viewWithTag:2341]; 

       [UIView animateWithDuration:0.5 animations:^{ 
//Change the frame as per your requirement 

        view.frame = CGRectMake(5, 5, 10, 70); 
        [lbl removeFromSuperview]; 
       }]; 
      } 
     } 
    } 

希望这有助于。