当presentviewcontroller或pushviewcontroller重新加载UITableView

时间:2016-05-20 09:25:29

标签: uitableview pushviewcontroller reloaddata presentviewcontroller

我有一些复杂细胞的uitableview,

当我按下单元格中的按钮时,如果我执行presentviewcontroller或pushviewcontroller,

它会自动重新加载数据,

我怎么能阻止它?

这是基础控制器

import UIKit

class Controller: UIViewController{
    override func viewDidLoad() {
       super.viewDidLoad();

       initView();
    }

    func initView(){
       self.view.backgroundColor = UIColor.whiteColor();
    }

    override func preferredStatusBarStyle() -> UIStatusBarStyle {
       return UIStatusBarStyle.LightContent;
    }
}

这是第二级

import Foundation

class LoadableController: Controller {

    var requestNum                      : Int = 0;

    func loadData(complete: (()->Void)? = nil){
        self.requestComplete();
    }

    func requestComplete(){
        if(self.requestNum == 0){
            requestFinished();
            return;
        }
        self.requestNum = self.requestNum > 0 ? self.requestNum - 1 : 0;
        if(self.requestNum == 0){
            requestFinished();
        }
    }

    func requestFinished(){}
}

这是第三级

class TableListViewController: LoadableController, UITableViewDelegate, UITableViewDataSource {

    var tableView           : UITableView?;
    var curPage             : Int = 0;
    var totalPage           : Int = 0;

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func initView() {
        super.initView();

        tableView = UITableView();
        tableView!.delegate = self;
        tableView!.dataSource = self;
        tableView!.delaysContentTouches = false;
        tableView!.canCancelContentTouches = true;
        self.view.addSubview(tableView!);
        tableView!.snp_makeConstraints { [unowned self](make) in
            make.edges.equalTo(self.view);
        }

        let header = EGRefresh(refreshingTarget: self, refreshingAction: #selector(self.loadData));
        header.setImages(Global.LOADING_IMAGES, duration: 0.16, forState: .Idle);
        header.setImages(Global.LOADING_IMAGES, duration: 0.16, forState: .Pulling);
        header.setImages(Global.LOADING_IMAGES, duration: 0.16, forState: .WillRefresh);
        header.setImages(Global.LOADING_IMAGES, duration: 0.16, forState: .Refreshing);
        header.lastUpdatedTimeLabel.hidden = true;
        tableView!.mj_header = header;

        let footer = MJRefreshBackNormalFooter.init(refreshingTarget: self, refreshingAction: #selector(self.loadNext));
        tableView!.mj_footer = footer;
    }

    func loadNext(){
        tableView!.mj_footer.endRefreshing();
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("ListCell", forIndexPath: indexPath);
        return cell;
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1;
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1;
    }

    override func requestFinished() {
        tableView!.mj_header.endRefreshing();
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    deinit{
        DDLogInfo("TableListViewController deinit");
    }

}

这里是tableview自动重新加载的地方。

import UIKit

class CircleViewController: TableListViewController {

    override func viewDidLoad() {
        super.viewDidLoad();
    }

    override func initView(){
        super.initView();

        self.title = "Circle";

        let items = ["One", "Two"];
        let segment = UISegmentedControl(items: items);
        segment.selectedSegmentIndex = 0;
        segment.setWidth(80, forSegmentAtIndex: 0);
        segment.setWidth(80, forSegmentAtIndex: 1);
        self.navigationItem.titleView = segment;

        tableView!.estimatedRowHeight = 50;
        tableView!.rowHeight = UITableViewAutomaticDimension;
//        tableView!.contentInset = UIEdgeInsetsMake(Dimens.TopBarHeight, 0, 0, 0);
        tableView!.registerClass(TextOnlyCell.self, forCellReuseIdentifier: "CircleTextOnlyCell");
        tableView!.registerClass(ImageAndPageCell.self, forCellReuseIdentifier: "CircleImageAndPageCell");

        Event.addEventListener(self, selector: #selector(self.goComment(_:)), name: EventType.CIRCLE_COMMENT, object: nil);
    }

    func goComment(ns: NSNotification) {
        let commentView = PostCommentViewController();
        self.navigationController?.pushViewController(commentView, animated: true);
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension;
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10;
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true);
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1;
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("CircleImageAndPageCell", forIndexPath: indexPath) as? ImageAndPageCell;
        if cell == nil {
            cell = ImageAndPageCell.init(style: .Default, reuseIdentifier: "CircleImageAndPageCell");
        }
        let images = [UIImage(named: "b.jpg")!, UIImage(named: "a.jpg")!, UIImage(named: "c.jpg")!, UIImage(named: "a.jpg")!];
        cell?.initView(nil, nickname: "im a nickname", time: "10 min", content: NSMutableAttributedString(string: "asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdf"), likeNum: 10, commentNum: 12, shareNum: 13, totalLine: 20, images: images, cellIndex: indexPath.row, hasPage: true, pageIcon: UIImage(named: "c.jpg")!, titleStr: "ahahaha");
        return cell!;
    }

    override func loadData(complete: (() -> Void)?) {
        self.requestComplete();
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    deinit {
        Event.removeAllEventListener(self);
        DDLogInfo("CircleViewController deinit");
    }
}

如果调用goComment,

将推送viewcontroller,

然后tableview将跳转到顶部,

我为cellForRowAtIndexPath添加了一个断点,

它被称为。

然后我尝试了presentviewcontroller,

这个问题也发生了,

即使我在外部viewcontroller中执行了presentviewcontroller。

似乎没有重装数据,

它只是滚动到顶部。

1 个答案:

答案 0 :(得分:0)

看起来像桌面视图没有考虑帐户中的uitoolbar大小(44分)

在prepareForSegue中保存tableview偏移量:(将其保存在CGPoint属性中)

self.tableViewScrollOffset = self.tableView.contentOffset;

然后,在viewWillAppear:中,检查它是否已被修改。如果是,请恢复它。

  

if(self.tableView.contentOffset.y!= self.tableViewScrollOffset.y){           [self.tableView setContentOffset:self.tableViewScrollOffset];           self.tableViewScrollOffset = CGPointZero;       }