小编yas*_*r h的帖子

Swift - 从url获取文件大小

我正在使用documentPicker获取任何文档的url路径,然后上传到数据库.我选择文件(pdf,txt ..),上传工作但我想限制文件的大小.

 public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {

        self.file = url //url
        self.path = String(describing: self.file!) // url to string
        self.upload = true //set upload to true
        self.attachBtn.setImage(UIImage(named: "attachFilled"), for: .normal)//set image
        self.attachBtn.tintColor = UIColor.black //set color tint
        sendbtn.tintColor = UIColor.white //


        do
        {
            let fileDictionary = try FileManager.default.attributesOfItem(atPath: self.path!)
            let fileSize = fileDictionary[FileAttributeKey.size]
            print ("\(fileSize)")
        } 
        catch{
            print("Error: \(error)")
        }

    }
Run Code Online (Sandbox Code Playgroud)

我收到错误消息,此文件不存在,文档选择器保存文件以及如何获取其属性.

filesize ios swift uidocumentpickervc

18
推荐指数
5
解决办法
9375
查看次数

通过标识符设置表格视图单元格高度 - Swift

我试图根据标识符设置表格视图中每个单元格的高度。我不想使用 indexPath.row,因为我正在从包含每个单元格类型标志的数组中填充单元格,并且顺序是随机的。正好 3 种类型的细胞。

我使用了这段代码,但它导致了一个错误:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    let cell = tableV.cellForRow(at: indexPath)
    if(cell?.reuseIdentifier == "homeFeaturedR"){
        return 200
    }else{
       return 360
    }
}
Run Code Online (Sandbox Code Playgroud)

错误在这一行:

let cell = tableV.cellForRow(at: indexPath)
Run Code Online (Sandbox Code Playgroud)

谢谢你。

height row uitableview ios swift

3
推荐指数
1
解决办法
1046
查看次数

重新加载后,在UITableView中保持单元格位置

我每隔几分钟刷新一次我的UITable,如果有新数据,它们应该在表的开头添加而不是重新加载.

var j = 0
let n = store?.news?.count
while(j < n!){
self.news?.insert((store?.news?[j])!, at: j)
    j = j + 1
}
Run Code Online (Sandbox Code Playgroud)

在第一个位置保存数据没问题.问题是我不能保持单元格位置在每次重新加载后向上移动(添加的记录数),我已经尝试了多个功能但显然它只适用于表格末尾添加的数据.

let contentOffset = self.tableV.contentOffset
self.tableV.reloadData()
self.tableV.contentOffset = contentOffset
Run Code Online (Sandbox Code Playgroud)

此外,我厌倦了保存单元格位置并添加输入数据的计数,但这种添加不起作用.有没有办法添加计数的输入数据的数量,让N对indexPath.row?

uitableview ios swift

0
推荐指数
1
解决办法
2877
查看次数

在iOS应用中显示通知当应用程序在前景/活动在swift 3

我正在尝试在应用处于活动状态时显示横幅通知.我使用了给定的方法但没有结果,横幅通知仅在应用关闭时出现:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    print("Push notification received: \(userInfo)")

    if application.applicationState == .active {
       print("active")
       let localNotification = UILocalNotification()
       localNotification.userInfo = userInfo
       localNotification.alertAction = "Test"
       localNotification.soundName = UILocalNotificationDefaultSoundName
       localNotification.alertBody = "Notification test!!"
       localNotification.fireDate = Date()
       UIApplication.shared.scheduleLocalNotification(localNotification)
     }
}
Run Code Online (Sandbox Code Playgroud)

它打印"活动"但通知未显示.我错过了任何一步吗?

谢谢.

push-notification apple-push-notifications ios swift

0
推荐指数
1
解决办法
6257
查看次数