ios - Q: Search Bar and Search Display Controller in storyboard with swift and Xcode8.0 crash at Appdelegate -
storyboard layout below:
this demo, , don't know error comes out.
in viewcontroller
:
import uikit import foundation class viewcontroller: uiviewcontroller { @iboutlet weak var tableview: uitableview! @iboutlet var searchdisplay: uisearchdisplaycontroller! // 所有组件 var ctrls:[string] = ["label","button1-初级","button1-高级","button2-初级","button2-高级","switch"] // 搜索匹配的结果,table view使用这个数组作为datasource var ctrlsel:[string] = [] override func viewdidload() { super.viewdidload() // 起始加载全部内容 self.ctrlsel = self.ctrls // 注册tableviewcell self.searchdisplay.searchresultstableview.register(uitableviewcell.self, forcellreuseidentifier: "swiftcell") // 设置文本提示 self.searchdisplay.searchbar.placeholder = "输入搜索信息" // 可以设置初始值 //self.searchdisplay.searchbar.text = "b" // 设置搜索栏提示信息 self.searchdisplay.searchbar.prompt = "搜索组件名称" // 不显示search bar边框 self.searchdisplay.searchbar.searchbarstyle = uisearchbarstyle.minimal // 显示分段条 self.searchdisplay.searchbar.showsscopebar = true self.searchdisplay.searchbar.scopebuttontitles = ["全部","初级","高级"] } // 返回表格行数(也就是返回控件数) func tableview(tableview: uitableview!, numberofrowsinsection section: int) -> int { return self.ctrlsel.count } // 创建各单元显示内容(创建参数indexpath指定的单元) func tableview(tableview: uitableview!, cellforrowatindexpath indexpath: nsindexpath!) -> uitableviewcell! { // 为了提供表格显示性能,已创建完成的单元需重复使用 let identify:string = "swiftcell" // 同一形式的单元格重复使用,在声明时已注册 let cell = self.searchdisplay.searchresultstableview.dequeuereusablecell( withidentifier: identify, for: indexpath indexpath) uitableviewcell cell.accessorytype = uitableviewcellaccessorytype.disclosureindicator cell.textlabel?.text = self.ctrlsel[indexpath.row] return cell } // 搜索代理uisearchbardelegate方法,每次改变搜索内容时都会调用 func searchbar(searchbar: uisearchbar!, textdidchange searchtext: string!) { self.searchtext = searchtext searchctrls() } // 选择分段条时调用 func searchbar(searchbar: uisearchbar!, selectedscopebuttonindexdidchange selectedscope: int) { print(selectedscope) searchctrls(); } // 保存搜索内容 var searchtext:string = "" // 搜索过滤 func searchctrls() { // 没有搜索内容时显示全部组件 if self.searchtext == "" { self.ctrlsel = self.ctrls } else { var scope = self.searchdisplay.searchbar.selectedscopebuttonindex; // 匹配用户输入内容的前缀 self.ctrlsel = [] ctrl in self.ctrls { let lc = ctrl.lowercased() if lc.hasprefix(self.searchtext) { if (scope == 0 || (scope == 1 && lc.hassuffix("初级")) || (scope == 2 && lc.hassuffix("高级"))) { self.ctrlsel.append(ctrl) } } } } // 不需要刷新table view显示 // self.searchdisplay.searchresultstableview.reloaddata() } }
crash @ appdelegate. in swift3
, xcode 8.0
environment. convenient search result ,use search bar , search display controller in storyboard
.
when run application, crash @ :class appdelegate: uiresponder, uiapplicationdelegate
line inappdelegate.swift
.the break info : thread 1: breakpoint 2.1
edit:
after set datasource
, delegate
of tableview vc
: there comes strange scenario:the searchbar
under tableview
i believe have set datasource , delegate of tableview incorrectly, please set datasource , delegate either in interface builder shown in below screenshot,
select tableview , set data source in connection inspector.
or can via code, setting them in viewdidload() method. paste these lines in viewdidload()
tableview.datasource = self tableview.delegate = self
as can see code snippet you've pasted, you've implemented uitableviewdelegate & uitableviewdatasource methods. you've not mentioned class conforms these protocols modify class declaration , specify class conforms these protocols.
class viewcontroller: uiviewcontroller, uitableviewdelegate, uitableviewdatasource { //your class implementation goes here }
Comments
Post a Comment