Ios 如何更改UITableViewCell的宽度?
我正在开发一个具有SwipeGesture的UITableView。 滑动时,单元格应向左滑动,设置为320的对象应出现将它们放置在UITableViewCell的contentView中,并在contentView上制作动画,将帧更改为x=-320,这样就可以工作。UITableView在将单元格添加到表视图时更改单元格的帧。如果要更改单元格的宽度,应更改表的宽度,或更改单元格中contentView的宽度 实现这一点的更好方法是子类化UITableViewCell并重写其-setFrame:方法,如下所示: 试试这个:Ios 如何更改UITableViewCell的宽度?,ios,uitableview,Ios,Uitableview,我正在开发一个具有SwipeGesture的UITableView。 滑动时,单元格应向左滑动,设置为320的对象应出现将它们放置在UITableViewCell的contentView中,并在contentView上制作动画,将帧更改为x=-320,这样就可以工作。UITableView在将单元格添加到表视图时更改单元格的帧。如果要更改单元格的宽度,应更改表的宽度,或更改单元格中contentView的宽度 实现这一点的更好方法是子类化UITableViewCell并重写其-setFrame:
- (void)setFrame:(CGRect)frame
{
frame.origin.x += inset;
frame.size.width -= 2 * inset;
[super setFrame:frame];
}
请尝试下面的代码
// 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];
>
>
希望这有帮助。尝试阅读本教程。这将消除您的所有疑问和问题。获取错误UITableViewController的无可见界面声明选择器setFrame:否则,您必须在单元格上添加视图,并且当您要向左滑动时,您必须从右向左设置该视图的动画
-(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];
}];
}
}
}