Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不带搜索栏的Swift filter UITableView_Uitableview_Swift_Ios8_Xcode6.3 - Fatal编程技术网

不带搜索栏的Swift filter UITableView

不带搜索栏的Swift filter UITableView,uitableview,swift,ios8,xcode6.3,Uitableview,Swift,Ios8,Xcode6.3,我有一个UITableView,我想根据从“滑动面板视图控制器”中选择的内容进行筛选。这是从面板获取返回值的函数 func itemSelected(type: Item) { self.selectedItem = Item.title delegate?.collapseSidePanels?() } 表视图代码 var myData: Array<AnyObject> = [] var selectedItem:Array<AnyObject>

我有一个UITableView,我想根据从“滑动面板视图控制器”中选择的内容进行筛选。这是从面板获取返回值的函数

func itemSelected(type: Item) {

    self.selectedItem = Item.title

    delegate?.collapseSidePanels?()
}
表视图代码

var myData: Array<AnyObject> = []
var selectedItem:Array<AnyObject> = []

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellID: NSString = "Cell"
    var Cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellID as String) as! UITableViewCell
    var data: NSManagedObject = myData[indexPath.row] as! NSManagedObject
    if tableView == selectedItem {
        data = self.selectedItem[indexPath.row] as! NSManagedObject
     } else
    {
       data = myData[indexPath.row] as! NSManagedObject
    }

    Cell.textLabel?.text = data.valueForKeyPath("itemname") as? String
    var tt = data.valueForKeyPath("itemtype") as! String
    Cell.detailTextLabel?.text = ("Item Type: \(tt)")
    return Cell
}
var-myData:Array=[]
var selectedItem:Array=[]
重写func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
let cellID:NSString=“Cell”
变量单元格:UITableViewCell=tableView.dequeueReusableCellWithIdentifier(cellID为字符串)为!UITableViewCell
变量数据:NSManagedObject=myData[indexPath.row]as!NSManagedObject
如果tableView==selectedItem{
data=self.selectedItem[indexPath.row]作为!NSManagedObject
}否则
{
data=myData[indexPath.row]作为!NSManagedObject
}
Cell.textLabel?.text=data.valueForKeyPath(“itemname”)作为?字符串
var tt=data.valueForKeyPath(“itemtype”)作为!字符串
Cell.detailTextLabel?.text=(“项目类型:\(tt)”)
返回单元
}
我需要对itemtype进行筛选

编辑-仍不会过滤,因此以下是tableViewController的完整代码

import UIKit
import CoreData
import Foundation

@objc
protocol tableViewControllerDelegate {
optional func toggleLeftPanel()
optional func toggleRightPanel()
optional func collapseSidePanels()
}
class tableViewController: UITableViewController, NSFetchedResultsControllerDelegate, SidePanelViewControllerDelegate {
var delegate: tableViewControllerDelegate?
var myData: Array<AnyObject> = []
var myFilteredData: Array<AnyObject> = []


@IBAction func leftTapped(sender: AnyObject) {
    delegate?.toggleLeftPanel?()
}

// Use this to change table view to edit mode
// and to Change the title when clicked on.
// Make sure to have sender set as UIBarButtonItem
// or you can not change the title of the button.
 var condition: Bool = true
@IBAction func buttonEdit(sender: UIBarButtonItem) {
    if(condition == true) {
        tableView.editing = true
        sender.title = "Done"
        condition = false
    } else {
        tableView.editing = false
        sender.title = "Edit"
        condition = true
    }

}

let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
var fetchedResultController: NSFetchedResultsController = NSFetchedResultsController()


override func viewDidLoad() {
    super.viewDidLoad()

}

override func viewWillAppear(animated: Bool) {
// This is neeed when using panel view controller to show the bottom navbar.
self.navigationController?.setToolbarHidden(false, animated: true)
    // ref app del
    let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

    // Ref data
    let context: NSManagedObjectContext = appDel.managedObjectContext!
    let freq = NSFetchRequest(entityName: "Products")
    myData = context.executeFetchRequest(freq, error: nil)!

}

override func viewDidAppear(animated: Bool) {

}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    if (self.myFilteredData.count != 0) {
        return self.myFilteredData.count
    } else {
        return self.myData.count
    }
}

func getFetchedResultController() -> NSFetchedResultsController {
    fetchedResultController = NSFetchedResultsController(fetchRequest: NSFetchRequest(), managedObjectContext: managedObjectContext!, sectionNameKeyPath: nil, cacheName: nil)
    return fetchedResultController
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellID: String = "Cell"
    var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellID as String) as! UITableViewCell
    var data: NSManagedObject
    if (self.myFilteredData.count != 0){
        data = myFilteredData[indexPath.row] as! NSManagedObject
        cell.textLabel?.text = data.valueForKeyPath("productname") as? String
        var tt = data.valueForKeyPath("itemtype") as! String
        cell.detailTextLabel?.text = ("Item J Type: \(tt)")
    } else {
        data = myData[indexPath.row] as! NSManagedObject
        cell.textLabel?.text = data.valueForKeyPath("productname") as? String
        var tt = data.valueForKeyPath("itemtype") as! String
        cell.detailTextLabel?.text = ("Item Type: \(tt)")
    }


    return cell
}

override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}

override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
    let item: AnyObject = myData[sourceIndexPath.row]
    myData.removeAtIndex(sourceIndexPath.row)
    myData.insert(item, atIndex: destinationIndexPath.row)
}

// called when a row deletion action is confirmed
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        switch editingStyle {
        case .Delete:
            // remove the deleted item from the model
            let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            let context:NSManagedObjectContext = appDel.managedObjectContext!
            context.deleteObject(myData[indexPath.row] as! NSManagedObject)
            myData.removeAtIndex(indexPath.row)
            context.save(nil)

            // remove the deleted item from the `UITableView`
            self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

        default:
            return

        }
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "showProduct"){
        let selectedIndexPath:NSIndexPath = self.tableView.indexPathForSelectedRow()!
        let genView:genViewController = segue.destinationViewController as! genViewController
        genView.row = selectedIndexPath.row

    }
    else if (segue.identifier == "addProduct"){

    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func itemSelected(item: Type) {
    var selectedType = item.title


    delegate?.collapseSidePanels?()

    for (key, value) in enumerate(self.myData) {
        if (value.valueForKeyPath("itemtype") !== "selectedType") {
            self.myFilteredData.append(value)
            dump(myFilteredData)
        } else {
            // do nothing with it
        }
    }

    tableView.reloadData() 
}
导入UIKit
导入CoreData
进口基金会
@objc
协议表ViewControllerDelegate{
可选功能切换leftpanel()
可选功能切换右面板()
可选函数collapseSidePanels()
}
类tableViewController:UITableViewController、NSFetchedResultsControllerDelegate、SidePanelViewControllerDelegate{
var委托:tableViewControllerDelegate?
var myData:Array=[]
var myFilteredData:Array=[]
@iAction func leftTapped(发送方:AnyObject){
代理?.toggleLeftPanel?()
}
//使用此选项可将表视图更改为编辑模式
//以及在单击时更改标题。
//确保将发送器设置为UIBarButtonItem
//或者您不能更改按钮的标题。
变量条件:Bool=true
@iAction func按钮编辑(发件人:UIBarButtonItem){
如果(条件==真){
tableView.editing=true
sender.title=“完成”
条件=假
}否则{
tableView.editing=false
sender.title=“编辑”
条件=真
}
}
让managedObjectContext=(UIApplication.sharedApplication().delegate为!AppDelegate)。managedObjectContext
var fetchedResultController:NSFetchedResultsController=NSFetchedResultsController()
重写func viewDidLoad(){
super.viewDidLoad()
}
覆盖功能视图将出现(动画:Bool){
//当使用“面板视图控制器”显示底部导航栏时,不需要这样做。
self.navigationController?.setToolbarHidden(假,动画:真)
//参考应用程序del
让appDel:AppDelegate=UIApplication.sharedApplication().delegate为!AppDelegate
//参考数据
让上下文:NSManagedObjectContext=appDel.managedObjectContext!
let freq=NSFetchRequest(entityName:“产品”)
myData=context.executeFetchRequest(频率,错误:nil)!
}
覆盖功能视图显示(动画:Bool){
}
//标记:-表视图数据源
重写func numberOfSectionsInTableView(tableView:UITableView)->Int{
//#警告可能不完整的方法实现。
//返回节数。
返回1
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
//#警告方法执行不完整。
//返回节中的行数。
如果(self.myFilteredata.count!=0){
返回self.myFilteredData.count
}否则{
返回self.myData.count
}
}
func getFetchedResultController()->NSFetchedResultsController{
fetchedResultController=NSFetchedResultsController(fetchRequest:NSFetchRequest(),managedObjectContext:managedObjectContext!,sectionNameKeyPath:nil,cacheName:nil)
返回fetchedResultController
}
重写func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
let cellID:String=“Cell”
变量单元格:UITableViewCell=tableView.dequeueReusableCellWithIdentifier(cellID为字符串)为!UITableViewCell
var数据:NSManagedObject
如果(self.myFilteredata.count!=0){
data=myFilteredData[indexPath.row]as!NSManagedObject
cell.textLabel?.text=data.valueForKeyPath(“productname”)作为?字符串
var tt=data.valueForKeyPath(“itemtype”)作为!字符串
cell.detailTextLabel?.text=(“项目J类型:\(tt)”)
}否则{
data=myData[indexPath.row]作为!NSManagedObject
cell.textLabel?.text=data.valueForKeyPath(“productname”)作为?字符串
var tt=data.valueForKeyPath(“itemtype”)作为!字符串
cell.detailTextLabel?.text=(“项目类型:\(tt)”)
}
返回单元
}
重写func tableView(tableView:UITableView,canMoveRowAtIndexPath:nsindepath)->Bool{
返回真值
}
重写func tableView(tableView:UITableView,MoveRowatineXpath sourceIndexPath:NSIndexPath,toIndexPath destinationIndexPath:NSIndexPath){
let item:AnyObject=myData[sourceIndexPath.row]
myData.removeAtIndex(sourceIndexPath.row)
插入(item,atIndex:destinationIndepath.row)
}
//确认行删除操作时调用
重写func tableView(tableView:UITableView,CommittedItingStyle editingStyle:UITableViewCellEditingStyle,forRowAtIndexPath indexPath:NSIndexPath){
切换编辑方式{
案例.删除:
//从模型中删除已删除的项
让appDel:AppDelegate=UIApplication.sharedApplication().delegate为!AppDelegate
让上下文:NSManagedObjectContext=appDel.managedObjectContext!
context.deleteObject(myData[indexPath.row]as!NSManagedObject)
myData.removeAtIndex(indexPath.row)
context.save(无)
//从“UITableView”中删除已删除的项目`
self.tableView.deleteRowsatindExpath([indexPath],withRowAnimation:.Fade)
违约:
返回
}
}
覆盖功能预设置(segue:UIStoryboardSegue,
var myFilteredData: Array<AnyObject> = []

func itemSelected(type: Item) {
    self.selectedItem = Item.title
    delegate?.collapseSidePanels?()

    for (key, value) in enumerate(self.myData) {                 
        if (value.valueForKeyPath("itemtype") == "yourCondition") {
            self.myFilteredData.append(value)
        } else {
            // do nothing with it
        }
    }

    tableView.reloadData() // use tableView.reloadSections with rowAnimation for better effect.
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    ...

    var data:NSManagedObject

    if (self.myFilteredData.count != 0) {
        data = myFilteredData[indexPath.row] as! NSManagedObject
    } else {
        data = myData[indexPath.row] as! NSManagedObject
    }

    ...
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if (self.myFilteredData.count != 0) {
        return self.myFilteredData.count
    } else {
        return self.myData.count
    }
}