繁体   English   中英

如何显示通过滑动行在自定义tableview单元内创建的视图?

[英]How to show a view created inside the custom tableview cell by swiping the row?

我已经创建了具有自定义表格视图单元格的iPhone应用程序,并且还在cellForRowAtIndexPath创建了一个视图。 现在,当我在UITable滑动单元格时,我创建的视图应显示在特定的单元格中,所有其他单元格内容应保持不变。

我的代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
RKCardCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"RKCardCell"];

// Configure the cell...
if (cell == nil) {
    cell = [[RKCardCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RKCardCell"];
}
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeMethod:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[cell addGestureRecognizer:swipeGesture];

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(-320.0, 8.0, 300, 180)];
[view setBackgroundColor:[UIColor greenColor]];

cell.profileImage.image = [UIImage imageNamed:[photoArray objectAtIndex:indexPath.row]];
cell.titleLabel.text = [titleArray objectAtIndex:indexPath.row];
cell.nameLabel.text = [nameArray objectAtIndex:indexPath.row];
cell.descriptionLabel.text = [descriptionArray objectAtIndex:indexPath.row];

//%%% I made the cards pseudo dynamic, so I'm asking the cards to change their frames depending on the height of the cell
cell.cardView.frame = CGRectMake(10, 5, 300, [((NSNumber*)[cardSizeArray objectAtIndex:indexPath.row])intValue]-10);

return cell;
}

以下代码用于手势滑动:

- (void)swipeMethod:(UIGestureRecognizer *)gestureRec
{
[UIView animateWithDuration: 0.5 animations:^{
    CGRectOffset(self.rkCardCell.cardView.frame, -320.0, 0.0);
}];

}

提前致谢。

如果将手势识别器与表格视图一起使用,则效果很好。

UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeTarget:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[cell addGestureRecognizer:_myTableView];

和您的滑动目标方法如下。

- (IBAction)swipeTarget:(UISwipeGestureRecognizer *)gestureRecognizer
{
    CGPoint swipePoint         = [gestureRecognizer locationInView:_myTableView];
    NSIndexPath *cellIndexPath = [_myTableView indexPathForRowAtPoint:swipePoint];
    RKCardCell *cell           = [_myTableView cellForRowAtIndexPath:cellIndexPath];

    // Add your code here to show your view
    [UIView animateWithDuration: 0.5 animations:^{
           CGRectOffset(cell.cardView.frame, -320.0, 0.0);
    }];
}

看看https://github.com/CEWendel/SWTableViewCell

它是制作自定义可滑动tableviewcell的一个非常好的简单库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM