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

时间:2014-11-19 11:39:05

标签: ios objective-c iphone uitableview

我创建了具有自定义tableview单元格的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);
}];

}

提前致谢。

2 个答案:

答案 0 :(得分:1)

如果您将手势识别器与表格视图一起使用,那就太好了。

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

答案 1 :(得分:0)

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

这是一个非常好且简单的库,可以制作自定义可转换的tableviewcell。