javascript - 在 tableView Swift 中使用 javascript 数据

标签 javascript ios xcode swift uitableview

你好,我是快速编程新手,遇到以下问题。我在我的操作扩展中读到了如何从 Safari 获取数据。使用 viewDidLoad 中的代码。到目前为止一切顺利。

我通过教程构建了一个 tableView,它工作得很好。

知道我想使用从 safari 中提取的数据到我的 tableView 中。问题是我的 tableView 首先加载,然后我的数据从 safari 中提取。

如何直接在 TableView 中使用数据?

这是我的代码。请注意,我是新手,它正在 build 中。 tableVie 现在已填充了教程中演示的信息。

import UIKit

导入MobileCoreServices

var webDataArray : [字符串] = []

ActionViewController 类:UIViewController、UITableViewDataSource、UITableViewDelegate {

@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var myTableView: UITableView!

var arrayOfWebData: [CustomCellContents] = [CustomCellContents]()
var effectStannd = false

override func viewDidLoad() {
    super.viewDidLoad()

    for item: AnyObject in self.extensionContext!.inputItems {
        let inputItem = item as! NSExtensionItem
        for provider: AnyObject in inputItem.attachments! {
            let itemProvider = provider as! NSItemProvider
            if itemProvider.hasItemConformingToTypeIdentifier(kUTTypePropertyList as! String) {
                itemProvider.loadItemForTypeIdentifier(kUTTypePropertyList as! String, options: nil, completionHandler: {(list, error) in
                    if let results = list as? NSDictionary {
                            NSOperationQueue.mainQueue().addOperationWithBlock {
                                var webData = results.description
                                webDataArray = split(webData) {$0 == ","}
                                var testArray = split(webDataArray[0]) {$0 == " "}
                                webDataArray.removeAtIndex(0)
                                webDataArray.append(testArray[7])
                                testArray = split(webDataArray[13]) {$0 == " "}
                                webDataArray.removeAtIndex(13)
                                webDataArray.append(testArray[1])
                                //println(webDataArray)
                                println(webDataArray.count)
                            }
                        }
                    })
            }
        }
    }
    println(webDataArray.count)
    self.setUpWebData()
    self.addEffect()
}


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

@IBAction func done() {
    // Return any edited content to the host app.
    // This template doesn't do anything, so we just echo the passed in items.
    self.extensionContext!.completeRequestReturningItems(self.extensionContext!.inputItems, completionHandler: nil)
}

func setUpWebData()
{
    var webData1 = CustomCellContents(fileName: "Torrent", fileKind: "img1.jpeg")
    var webData2 = CustomCellContents(fileName: "Magnet", fileKind: "img2.jpeg")
    var webData3 = CustomCellContents(fileName: "Exe", fileKind: "img1.jpeg")
    var webData4 = CustomCellContents(fileName: "DMG", fileKind: "img2.jpeg")

    arrayOfWebData.append(webData1)
    arrayOfWebData.append(webData2)
    arrayOfWebData.append(webData3)
    arrayOfWebData.append(webData4)
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return arrayOfWebData.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{

    let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomCell

    let CustomCellContents = arrayOfWebData[indexPath.row]

    cell.setCell(CustomCellContents.fileName, imageName: CustomCellContents.fileKind)

    return cell
}


/////// Custom swip from Right
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{

}


func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]?
{
    var shareAction = UITableViewRowAction(style: .Normal, title: "Download") { (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in

        let firstActivityItem = self.arrayOfWebData[indexPath.row]

        let activityViewControler = UIActivityViewController(activityItems: [firstActivityItem], applicationActivities: nil)

        self.presentViewController(activityViewControler, animated: true, completion: nil)
    }

shareAction.backgroundColor = UIColor.blueColor()

return [shareAction]

}


//// de volgende functie zorgt ervoor dat de rij automatisch word gedeselecteerd
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    self.myTableView.deselectRowAtIndexPath(indexPath, animated: true)
}


func addEffect(){
    if effectStannd{
        var effect = UIBlurEffect(style: UIBlurEffectStyle.Light)
        var effectView = UIVisualEffectView(effect: effect)
        effectView.frame = CGRectMake(0, 0, 320, 600)
        view.addSubview(effectView)
        effectStannd = false
    }
}

}

最佳答案

我找到了加载后刷新表格 View 的解决方案。像这样就可以正常工作了。

func didRefreshList(){
    var tableView = myTableView
    self.tableData = webDataArray3
    tableView.reloadData()
    self.refreshControl.endRefreshing()
}

关于javascript - 在 tableView Swift 中使用 javascript 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31662866/

相关文章:

c++ - Xcode C++ 开发,需要说明

ios - 代码签名资源规则路径在 Jenkins 上失败,CODE_SIGN_RESOURCE_RULES_PATH

javascript - 在 React 高阶组件中传播 props 的目的是什么?

javascript - 将对象中的所有数组合并为一个数组。

ios - 从 Apple 服务器获取日期和时间

ios - 分配parentGroup时CNContactViewController为空

javascript - 在表行上显示 div onmouseover,并使用行属性动态设置文本和 URL

javascript - 每秒使用 cron 作业执行脚本的替代方案?

ios - 如何注册 iOS safari 应该支持的文档类型?

ios - 如何将数据从 View Controller 传递到导航 Controller ,然后再传递到另一个 View Controller ?