如何改变UITableViewCell的宽度?

时间:2014-09-24 06:27:09

标签: ios uitableview

我正在使用UITableView SwipeGesture。 在滑动时,单元格应向左滑动,并且应设置为320的对象

3 个答案:

答案 0 :(得分:0)

将它们放在contentView的{​​{1}}中,然后在UITableViewCell上制作一个动画,将帧更改为x = -320,这样就可以了。

答案 1 :(得分:0)

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

实现这一目标的更好方法是继承UITableViewCell并覆盖其-setFrame:方法,如下所示:

试试这个:

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

答案 2 :(得分:0)

请尝试以下代码。

    // 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];
                }];
            }
        }
    }

希望这有帮助。