-- -- 定义类 -- waxClass{"MyListController", UITableViewController, protocols = {"UITableViewDelegate", "UITableViewDataSource"}} -- -- 初始化 -- function init(self) self.super:init() self.states = {} -- {"Michigan", "California", "New York", "Illinois", "Minnesota", "Florida"} -- loadData(self) self.loadingMore = false self.page = 1 return self end -- -- 页面加载 -- function viewDidLoad(self) self:tableView():setDataSource(self) self:tableView():setDelegate(self) self:setTitle("导航标题") self:createTableFooter() -- 添加下拉事件(IOS6新特性) -- self:setRefreshControl(UIRefreshControl:init()) -- self:refreshControl():addTarget_action_forControlEvents(self, "refreshTable", UIControlEventValueChanged) end -- -- -- function numberOfSectionsInTableView(self, tableView) return 1 end -- -- -- function tableView_numberOfRowsInSection(self, tableView, section) return #self.states end -- -- 调整TableView的大小 -- function viewDidAppear(self, animated) self:tableView():setFrame(CGRect(0, 20, 320, 400)) end -- -- -- function tableView_cellForRowAtIndexPath(self, tableView, indexPath) local identifier = "TableViewCell" local cell = tableView:dequeueReusableCellWithIdentifier(identifier) cell = cell or UITableViewCell:initWithStyle_reuseIdentifier(UITableViewCellStyleDefault, identifier) local record = self.states[indexPath:row() + 1]; cell:textLabel():setText(record["appname"]) local image_file = record["appicon"] local myImage =UIImage:imageWithData(NSData:dataWithContentsOfURL(NSURL:URLWithString(image_file))); cell:imageView():setImage(myImage) return cell end -- -- 刷新触发的事件 -- function refreshTable(self) self:refreshControl():setAttributedTitle(NSAttributedString:alloc():initWithString("刷新中...")); puts("refresh_tablerefresh_tablerefresh_tablerefresh_tablerefresh_tablerefresh_table") self:performSelector_withObject_afterDelay("loadData", nil, 2.0); end -- -- -- function tableView_didSelectRowAtIndexPath(self, tableView, indexPath) tableView:deselectRowAtIndexPath_animated(indexPath, true) end -- -- -- function scrollViewDidEndDecelerating(self) puts("scrollViewDidEndDecelerating") end -- 获取列表数据 function loadData(self, nextPage) -- self:refreshControl():endRefreshing() -- self:refreshControl():setAttributedTitle(NSAttributedString:alloc():initWithString("下拉刷新")) local headers = {} local url = "http://myUrl/list/"..self.page..".json" print(url) wax.http.request{headers=headers, url, callback = function(body, response) -- request is just a NSHTTPURLResponse if 200 == response:statusCode() then -- Since the content-type is json, Wax automatically parses it and places -- it into a Lua table local tab_service = body['service'] if tab_service then local statusCode = tab_service['statusCode'] local resultSet = tab_service['resultSet'] print("statusCode=", statusCode) if "000000" == statusCode then local apps = resultSet['app'] -- for k,v in pairs(apps) do -- table.insert(self.states, v) -- end self.states = apps self:tableView():reloadData() if nextPage then self.page = self.page + 1 else if self.page <= 1 then self.page = 1 else self.page = self.page - 1 end end self.loadingMore = false; self:createTableFooter() end end end self.loadingMore = false; end } -- self.loadingMore = false; end -- -- 创建表格底部 -- function loadDataBegin(self, nextPage) puts(nextPage) if not self.loadingMore then self.loadingMore = true local tableFooterActivityIndicator = UIActivityIndicatorView:initWithFrame(CGRect(75.0, 10.0, 20.0, 20.0)); tableFooterActivityIndicator:setActivityIndicatorViewStyle(UIActivityIndicatorViewStyleGray); tableFooterActivityIndicator:startAnimating() self:tableView():tableFooterView():addSubview(tableFooterActivityIndicator); self:loadData(nextPage) end end -- -- 加载数据完毕 -- function loadDataEnd() self.loadingMore = false; self:createTableFooter() end -- -- 创建表格底部 -- function createTableFooter(self) self:tableView():setTableFooterView(nil) local tableFooterView = UIView:initWithFrame(CGRect(0.0, 0.0, self:tableView():bounds().width, 40.0)) local loadMoreText = UILabel:initWithFrame(CGRect(0.0, 0.0, 116.0, 40.0)) loadMoreText:setCenter(tableFooterView:center()) loadMoreText:setFont(UIFont:fontWithName_size("Helvetica Neue", 14.0)) loadMoreText:setText("上拉显示更多数据") tableFooterView:addSubview(loadMoreText) self:tableView():setTableFooterView(tableFooterView) end -- -- 滚动式触发 -- function scrollViewDidEndDragging_willDecelerate(self, decelerate) puts("scrollViewDidEndDragging_willDecelerate") local table_y = self:tableView():contentOffset().y local table_h = self:tableView():contentSize().height local frame_h = self:tableView():frame().height print(self.loadingMore, table_y, table_h, frame_h, "((not self.loadingMore) and (table_y > (table_h - frame_h)))=="..tostring(((not self.loadingMore) and (table_y > (table_h - frame_h))))) -- 上拉到底部刷新 if((not self.loadingMore) and (table_y > (table_h - frame_h))) then self:loadDataBegin(true) end -- 下拉到顶部刷新 if((not self.loadingMore) and table_y < -5 ) then self:loadDataBegin(false) end end