刷新自动UITableVIew单元格(编辑)

时间:2016-09-29 18:06:28

标签: ios iphone uitableview

有没有办法使UITableView单元格加载,向右滑动以显示编辑功能的存在。我想这样做是为了教育我的应用程序中的用法,它可以在其中一个具有该功能的tableViews中完成。

e.g。我已经附加了一个屏幕短的邮件应用程序与一个单元格刷,并希望我的应用程序加载与已经刷过的单元格。

enter image description here

提前致谢。 ;)

下面的AnimateCell ObjectiveC答案的Swift转换

var cell:UITableViewCell?

func animateCell(){

let indexPath = NSIndexPath(row: 2, section: 1)



cell = self.tableView.cellForRow(at: (indexPath as NSIndexPath) as IndexPath)

//  The crash occurs here with lldb
var aLabel = UILabel(frame: CGRect(x: (cell?.bounds.size.width)!, y: 0, width: 200, height: (cell?.bounds.height)!))
aLabel.text! = "Delete"
aLabel.backgroundColor = UIColor.red
aLabel.textColor = UIColor.white
cell?.addSubview(aLabel)

UIView.animate(withDuration: 0.3, animations: {() -> Void in
    self.cell?.frame = CGRect(x: (self.cell?.frame.origin.x)! - 100, y: (self.cell?.frame.origin.y)!, width: (self.cell?.bounds.size.width)!, height: (self.cell?.bounds.size.height)!)

    }, completion: {(finished: Bool) -> Void in
        UIView.animate(withDuration: 0.5, animations: {() -> Void in

            self.cell?.frame = CGRect(x: (self.cell?.frame.origin.x)! + 100, y: (self.cell?.frame.origin.y)!, width: (self.cell?.bounds.size.width)!, height: (self.cell?.bounds.size.height)!)

            }, completion: {(finished: Bool) -> Void in
                aLabel.removeFromSuperview()
                aLabel = nil
        })
})

}

2 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情:

-(void)animateCell{

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

    __block UILabel *aLabel = [[UILabel alloc]initWithFrame:CGRectMake(cell.bounds.size.width,
                                                                           0,
                                                                           200,
                                                                           cell.bounds.size.height)];

    aLabel.text = @"Delete";
    aLabel.backgroundColor = [UIColor redColor];
    aLabel.textColor = [UIColor whiteColor];
    [cell addSubview:aLabel];

    [UIView animateWithDuration:0.3 animations:^{
        [cell setFrame:CGRectMake(cell.frame.origin.x - 100, cell.frame.origin.y, cell.bounds.size.width, cell.bounds.size.height)];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5 animations:^{
            [cell setFrame:CGRectMake(cell.frame.origin.x + 100, cell.frame.origin.y, cell.bounds.size.width, cell.bounds.size.height)];
        } completion:^(BOOL finished) {
            [aLabel removeFromSuperview];
            aLabel = nil;
        }];
    }];

}

您可以在短时间后在viewDidAppear中调用此方法..

<强>更新

对于Swift 3,你可以使用类似的参考:

func animateCell() {
        var cell : UITableViewCell?
        let indexPath = NSIndexPath(row: 1, section: 0)
        cell = self.tableView.cellForRow(at: (indexPath as NSIndexPath) as IndexPath)

        var aLabel : UILabel! = UILabel()
        //aLabel.translatesAutoresizingMaskIntoConstraints = false
        aLabel.frame = CGRect(x: (cell?.bounds.size.width)!, y: 0, width: 400, height: (cell?.bounds.height)!)
        cell?.clipsToBounds = false
        print(aLabel.frame)
        aLabel!.text = "Delete"
        aLabel!.backgroundColor = UIColor.red
        print(aLabel.frame)
        aLabel.textColor = UIColor.white
        cell?.addSubview(aLabel)


        UIView.animate(withDuration: 0.5, animations: {
            cell?.frame = CGRect(x: (cell?.frame.origin.x)! - 100, y: (cell?.frame.origin.y)!, width: (cell?.bounds.size.width)!, height: (cell?.bounds.size.height)!)

            }) { (finished: Bool) in
                UIView.animate(withDuration: 0.5, animations: {
                    cell?.frame = CGRect(x: (cell?.frame.origin.x)! + 100, y: (cell?.frame.origin.y)!, width: (cell?.bounds.size.width)!, height: (cell?.bounds.size.height)!)

                    }, completion: { (finished: Bool) in
                        aLabel.removeFromSuperview()
                        aLabel = nil
                })
        }
    }

答案 1 :(得分:0)

您想将默认编辑模式设置为tableview。编辑模式是UITableView的状态。您可以使用以下命令以编程方式设置它:

[tableView setEditing:YES animated:YES]

无论是否有动画,都可以这样做。