当 presentviewcontroller 或 pushviewcontroller 时 UITableView 被重新加载
Posted
技术标签:
【中文标题】当 presentviewcontroller 或 pushviewcontroller 时 UITableView 被重新加载【英文标题】:UITableView is reloaded when presentviewcontroller or pushviewcontroller 【发布时间】:2016-05-20 09:25:28 【问题描述】:我有一个包含一些复杂单元格的 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,
视图控制器将被推送,
然后tableview会跳到顶部,
我在 cellForRowAtIndexPath 中添加了一个断点,
它被调用了。
然后我尝试了presentviewcontroller,
这个问题也出现过,
即使我在外部 viewcontroller 中做了 presentviewcontroller。
好像没有重新加载数据,
它只是滚动到顶部。
【问题讨论】:
请向我们展示您的代码,以便我们以更好的方式帮助您。 @DiogoAntunes 我正在编辑我的帖子,请帮助,谢谢! 【参考方案1】:它看起来像表格视图,没有考虑 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;
【讨论】:
这似乎是从 ios 8.0 到 8.2 的错误以上是关于当 presentviewcontroller 或 pushviewcontroller 时 UITableView 被重新加载的主要内容,如果未能解决你的问题,请参考以下文章
当 webview 呈现 ImagePicker 时未调用 presentViewController
当 presentViewController 没有导航视图控制器时,viewWillAppear 方向不正确
presentViewController 不显示标签栏或导航栏
当presentviewcontroller ios时标签栏控制器显示黑屏