NSString* urlStr = [strfstringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSetcharacterSetWithCharactersInString:@"`#%^{}\"[]|\\<> "].invertedSet];
Objective-C
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//1.得到cell
XWShopCell *cell = [XWShopCell cellWithTableView:tableView];
//2.传递模型
cell.wine = self.dataArray[indexPath.row];
//3.回传cell
return cell;
}
上面的的代码中
return self.dataArray.count;
其实就是利用
@property (nonatomic, strong) NSArray *dataArray;
@property 的特性,为属性生成了get和set方法,而这里是调用的get方法,但是上述代码中return self.dataArray.count 会调用
- (NSArray *)dataArray{ return _dataArray}
这样调用,如果成员属性dataArray 开始没有赋值的,那么在使用的时候,调用get方法,不重写的话,会报错,空指针,所以一般我们会重写get方法
//重写get方法
- (NSArray *)dataArray
{
if (nil == _dataArray){
_dataArray = [NSArray array];
}
return _dataArray
}
这样写,就防止了成员属性为没有赋值的情况
Swift
//MARK tablview的 dataSource 代理方法
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return self.dataArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
//1.得到cell
let cell = XWShopCell.cellWithTableView(tableView)
//2.传递模型
cell.wine = self.dataArray[indexPath.row]
//3.回传cell
return cell
}
而这句
return self.dataArray.count
//1.分析 NSArray 是一个闭包的返回值,而这是一个没有参数的闭包
lazy var dataArray:NSArray = { [] }()
//2.也可以写成这样 lazy var dataArray:NSArray = { return NSArray() }()
//3.从plist文件加载
lazy var dataArray:Array = {
let winePath = NSBundle.mainBundle().pathForResource("wine.plist", ofType: nil)!
let winesM = NSMutableArray(contentsOfFile: winePath);
var tmpArray:Array! = []
for tmpWineDict in winesM! {
var wine:XWWine = XWWine.wineWithDict(tmpWineDict as! NSDictionary)
tmpArray.append(wine)
}
print("我就运行一次")
return tmpArray }()
#import"ViewController.h"#import"AFNetworkReachabilityManager.h"@interfaceViewController ()@property(nonatomic, strong) NSURLSessionDownloadTask *downloadTask;// 下载任务@property(nonatomic, strong) NSURLSession *session;// 会话@property(nonatomic, strong) NSData *resumData;// 续传数据@property(nonatomic, strong) NSDate *lastDate;// 上次获取到数据的时间./** 两个label都是用Storybord创建的. */@property(weak, nonatomic) IBOutlet UILabel *progressLabel;@property(weak, nonatomic) IBOutlet UILabel *speedLabel;@end@implementationViewController
@end