我需要的帮助不大。 和平大家,希望你做得很好。
我用tableview创建了一个应用程序,我想在我的tableview中添加一个搜索栏,所以我找到了一个如何将搜索栏添加到tableview的教程,但在教程中它只是一个包含项目的列表但不是当你的项目时键入他们将您带到textcontroller的Viewcontroller的项目。与教程不同,我的应用程序是这样,当您键入其中一个项目时,他们会将您移动到带有textview的Viewcontroller。但更好的是你可以看到我的tablview如何: [![在此处输入图像说明] [1]] [1]
它看起来不错,当我使用搜索栏时看起来也不错:
[![在此处输入图像说明] [2]] [2]
但问题是,当你使用搜索时,它会给我不同变量的不同数据,如图片所示: [![在此处输入图像说明] [3]] [3]
希望我很清楚,希望有人能解释我如何解决这个问题。
和平。
这是我的tablview代码:
import UIKit
class TableViewController: UITableViewController, UISearchResultsUpdating {
var FirstTableArray = [String]()
var passThisArray = [String]()
var filteredVerbs = [String]()
var resultSearchController = UISearchController()
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = UISearchController(searchResultsController: nil)
self.resultSearchController.searchResultsUpdater = self
self.resultSearchController.dimsBackgroundDuringPresentation = false
self.resultSearchController.searchBar.sizeToFit()
self.tableView.tableHeaderView = self.resultSearchController.searchBar
self.tableView.reloadData()
// This array will display on your tableviewcell.
FirstTableArray = [åadlyde, åanbefale, åansette, åarbeide, åavslå, åavstå, åbade, åbanke, åbarbere, åbe, åbearbeide, åbedra, åbedøve, åbegynne, åbehandle, åbeite, åbeskrive, åbestemme…]
//You can pass element of this array
passThisArray = ["å adlyde", "å anbefale", "å ansette", "å arbeide", "å avslå", "å avstå", "å bade", "å banke", "å barbere", "å be", "å bearbeide", "å bedra", "å bedøve", "å begynne", "å behandle"…]
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(passThisArray.count > 3){
if (self.resultSearchController.active)
{
return self.filteredVerbs.count;
}
else
{
return self.passThisArray.count
}
}
return FirstTableArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let Cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
Cell.textLabel?.text = passThisArray[indexPath.row]
if (self.resultSearchController.active)
{
Cell.textLabel?.text = self.filteredVerbs[indexPath.row]
return Cell
}
else
{
Cell.textLabel?.text = self.passThisArray[indexPath.row]
return Cell
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "detailView") {
let vc = segue.destinationViewController as! ViewwController
//Get the Index of selected Cell
var indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!
//assign string to next view controller instance from selected cell.
vc.FirstString = FirstTableArray[indexPath.row]
if (self.resultSearchController.active) {
vc.FirstString = FirstTableArray[filteredVerbs.count]
}
}
}
func updateSearchResultsForSearchController(searchController: UISearchController)
{
self.filteredVerbs.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
let array = (self.passThisArray as NSArray).filteredArrayUsingPredicate(searchPredicate)
self.filteredVerbs = array as! [String]
self.tableView.reloadData()
}
}
答案 0 :(得分:0)
所以看来你的问题在于这个方法,在选定的行中:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "detailView") {
let vc = segue.destinationViewController as! ViewwController
//Get the Index of selected Cell
var indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!
//assign string to next view controller instance from selected cell.
vc.FirstString = FirstTableArray[indexPath.row]
if (self.resultSearchController.active) {
vc.FirstString = FirstTableArray[filteredVerbs.count] // <<---
}
}
}
如果搜索处于有效状态,您应该采用FirstString
这种方式self.filteredVerbs[indexPath.row]
。
来自评论的知识: 似乎没有简单的方法来映射存储在这两个数组中的值。
此外,这不是存储此数据的理想方式。它不灵活,并且将数据与呈现混合在一起,这通常应该避免。我建议创建一个存储数据的结构,如下所示:
class Flexion : NSObject {
var infinitiv:String = ""
var presens:String = ""
var preteritum:String = ""
var perfektum:String = ""
var english:String = ""
init(infinitiv:String, presens:String, preteritum:String, perfektum:String, english:String) {
self.infinitiv = infinitiv
self.presens = presens
self.preteritum = preteritum
self.perfektum = perfektum
self.english = english
}
}
在您的情况下,主要的好处是只有一个数组包含从中获取表视图的对象。整个事情也会变得更具可读性,并且更容易进一步保养。
所以原始问题的代码会变成这样:
class TableViewController: UITableViewController, UISearchResultsUpdating {
var FirstTableArray = [Flexion]()
var filteredVerbs = [Flexion]()
var resultSearchController = UISearchController()
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = UISearchController(searchResultsController: nil)
self.resultSearchController.searchResultsUpdater = self
self.resultSearchController.dimsBackgroundDuringPresentation = false
self.resultSearchController.searchBar.sizeToFit()
self.tableView.tableHeaderView = self.resultSearchController.searchBar
//create your objects like this :
var åadlyde = Flexion(infinitiv: "å adlyde", presens: "adlyder", preteritum: "adlød", perfektum: "har adlydt", english: "obey")
//create the rest of them
FirstTableArray = [åadlyde, åanbefale, åansette, åarbeide, åavslå, åavstå, åbade, åbanke, åbarbere, åbe, åbearbeide, åbedra, åbedøve, åbegynne, åbehandle, åbeite, åbeskrive, åbestemme…]
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (self.resultSearchController.active) {
return self.filteredVerbs.count
}
return FirstTableArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let Cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
if (self.resultSearchController.active) {
let verb = self.filteredVerbs[indexPath.row] as Flexion
Cell.textLabel?.text = verb.infinitiv
} else {
let verb = self.FirstTableArray[indexPath.row] as Flexion
Cell.textLabel?.text = verb.infinitiv
}
return Cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "detailView") {
let vc = segue.destinationViewController as! UIViewController
var indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!
if (self.resultSearchController.active) {
vc.verb = filteredVerbs[indexPath.row]
} else {
vc.verb = FirstTableArray[indexPath.row]
}
}
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
self.filteredVerbs.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF.infinitiv CONTAINS[c] %@", searchController.searchBar.text!)
let array = (self.FirstTableArray as NSArray).filteredArrayUsingPredicate(searchPredicate)
self.filteredVerbs = array as! [Flexion]
self.tableView.reloadData()
}
}
您还需要在"detailView"
视图控制器中进行必要的更改 - 添加verb
Flexion
类型的NSPredicate
变量,并更改向用户显示数据的方式。< / p>
这样就不需要这个&#34;映射&#34;数组之间。
注意:请注意infinitiv
中的更改 - 现在只检查该字词的public class MainDllProject
{
ISettings m_Settings;
public MainDllProject()
{
// Change this before compiling
this.m_Settings = new DebugSettings();
//this.m_Settings = new DeploySettings();
// use settings from the settings class
String setting1 = this.m_Settings.Setting1
Int32 setting2 = this.m_Settings.Setting2
//...
}
}
public interface ISettings
{
String Setting1 { get; }
Int32 Setting2 { get; }
}
public class DebugSettings: ISettings
{
public String Setting1
{ get { return "data_debug";} }
public Int32 Setting2
{ get { return 2;} }
}
public class DeploySettings: ISettings
{
public String Setting1
{ get { return "data_deploy";} }
public Int32 Setting2
{ get { return 1;} }
}
版本。