我正在尝试从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)
我的问题是:如果我想viewController在DataSource类中使用属性或调用类的方法,我该怎么办?例如,我想presentViewController在用户选择单元格时调用:
func tableView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
//do something here …Run Code Online (Sandbox Code Playgroud) 以下是代码未对齐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)
我有为每个单元格下载视频文件的表格视图。这是我下载视频文件的代码。
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)