小编bil*_*p22的帖子

如何从Swift中的viewController类中分离dataSource和delegate?

我正在尝试从viewController中分离dataSource和delegate,以防止viewController变得混乱.我阅读了一些帖子,发现我可以像下面一样分开dataSource,创建一个类来表示dataSource:

import UIKit

class DataSource: NSObject, UITableViewDataSource, UITableViewDelegate {

    var movies = [String]()

    //MARK: - UITableViewDataSource
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

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

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

        let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier", forIndexPath: indexPath) as UITableViewCell


        cell.textLabel?.text =  movies[indexPath.row]

        return cell
    }

}
Run Code Online (Sandbox Code Playgroud)

我的问题是:如果我想viewControllerDataSource类中使用属性或调用类的方法,我该怎么办?例如,我想presentViewController在用户选择单元格时调用:

func tableView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    //do something here …
Run Code Online (Sandbox Code Playgroud)

ios swift

5
推荐指数
1
解决办法
2401
查看次数

中心在UIView中对齐CAShape图层

以下是代码未对齐UIView中心的图像,白色.

CAShapeLayer *layer = [CAShapeLayer layer];
layer.anchorPoint=CGPointMake(0.5, 0.5);

layer.path=[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 75.0, 75.0)].CGPath;
layer.fillColor =[UIColor redColor].CGColor;

[self.shapeView.layer addSublayer:layer];
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

cashapelayer ios

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

取消一个带有特定 url 的 Alamofire 下载请求

我有为每个单元格下载视频文件的表格视图。这是我下载视频文件的代码。

 func beginItemDownload(id:String,url:String,selectRow:Int) {

    let pathComponent = "pack\(self.packID)-\(selectRow + 1).mp4"

    let destination: DownloadRequest.DownloadFileDestination = { _, _ in
      let directoryURL: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
      let folderPath: URL = directoryURL.appendingPathComponent("Downloads", isDirectory: true)
      let fileURL: URL = folderPath.appendingPathComponent(pathComponent)
      return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
    }
    let url = URL(string:url)

    Alamofire.download(
      url!,
      method: .get,
      parameters: nil,
      encoding: JSONEncoding.default,
      headers: nil,
      to: destination).downloadProgress(closure: { (progress) in
        DispatchQueue.main.async {

          if progress.fractionCompleted < 1 {

             print(Float(progress.fractionCompleted))

          }

          if progress.fractionCompleted == 1 {
            print("Completed")
          }
        }

      }).response(completionHandler: …
Run Code Online (Sandbox Code Playgroud)

ios swift alamofire

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

标签 统计

ios ×3

swift ×2

alamofire ×1

cashapelayer ×1