【发布时间】:2015-07-15 09:56:42
【问题描述】:
我正在使用 swift 和 Xcode 6.3 版构建 iOS 应用程序
我创建了一个表格视图,搜索栏作为一个出口。
之后我创建了一个新类 ProjectTableviewCell 作为 UITableview 单元格的子类。
在我的故事板中,我将多个标签连接到原型单元格。
我实现了搜索栏方法。它在 tableviewcell 中正确过滤数据。这一切都很好。
默认情况下,在我搜索任何内容之前,tableview 会显示内容列表。
过滤操作后,它只显示特定的过滤数组。
我现在面临的问题是表格视图单元格内容没有显示。
我的预期输出是我想在我的表格视图中显示过滤后的数组以及现有的单元格内容。
class myProjects: UIViewController,UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate {
@IBOutlet weak var projectTable: UITableView!
@IBOutlet weak var searchProject: UISearchBar!
var project = [String]()
var searchActive : Bool = false
var filtered:[String] = []
//Search Bar Methods
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchActive = true;
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
searchActive = false;
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
searchActive = false;
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchActive = false;
}
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
filtered = project.filter({ (text) -> Bool in
let tmp: NSString = text
let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
return range.location != NSNotFound
})
if(filtered.count == 0){
searchActive = false;
} else {
searchActive = true;
}
self.projectTable.reloadData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(searchActive) {
return filtered.count
}
return project.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let textCellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! projectTableViewCell
let row = indexPath.row
if(searchActive){
cell.textLabel?.text = filtered[indexPath.row]
} else {
(cell.contentView.viewWithTag(1) as! UILabel).text = project[row] as String
}
(cell.contentView.viewWithTag(3) as! UILabel).text = projectStatus[row] as String
(cell.contentView.viewWithTag(7) as! UILabel).text = ProjectDescription[row] as String
(cell.contentView.viewWithTag(12) as! UILabel).text = category[row] as String
(cell.contentView.viewWithTag(13) as! UILabel).text = subcategory[row] as String
(cell.contentView.viewWithTag(14) as! UILabel).text = project_Tags[row] as String
(cell.contentView.viewWithTag(15) as! UILabel).text = skill_Needed[row] as String
(cell.contentView.viewWithTag(16) as! UILabel).text = budget[row] as String
(cell.contentView.viewWithTag(17) as! UILabel).text = timeFrames[row] as String
(cell.contentView.viewWithTag(18) as! UILabel).text = location[row] as String
return cell
}
我是 Swift 初学者。非常感谢您的帮助。
【问题讨论】:
-
建议做一个Class的项目,而不是你拿的这些数的数组。
-
是啊,我刚才明白了,我换了一个班级。
标签: ios swift uitableview uisearchbar