码迷,mamicode.com
首页 > 其他好文 > 详细

UITableView(二)

时间:2016-10-04 09:52:21      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSArray *list = @[@"条目1",@"条目2"];
    self._dataList = list;
    
    UITableView *table
        = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    
    self._tableView = table;
    
    self._tableView.dataSource = self;
    self._tableView.delegate   = self;
    
    [self.view addSubview:self._tableView];
    
    NSOperationQueue *queue=[[NSOperationQueue alloc]init];
    self._queue = queue;
    
}

#pragma mark - Table view data source
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    
    if(cell == nil){
        
        //cell的四种样式:
        //UITableViewCellStyleDefault,      // 默认风格,自带标题和一个图片视图,图片在左
        //UITableViewCellStyleValue1,        // 只有标题和副标题 副标题在右边
        //UITableViewCellStyleValue2,        // 只有标题和副标题,副标题在左边标题的下边
        //UITableViewCellStyleSubtitle      // 自带图片视图和主副标题,主副标题都在左边,副标题在下
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        
    }
    
    
    
    NSString *url = @"http://XXXXX.com/article/uploadfile/2014/0905/20140905042806503.jpg";
    
    NSOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
        
        UIImage *image = [UIImage imageWithData:data];
        dispatch_async(dispatch_get_main_queue(), ^{
            printf("height = %f\r", image.size.height);
            printf("width = %f\r", image.size.width);
                        
            cell.imageView.image = image;
        });
    }];
    
    
    [self._queue addOperation:operation];
    
    cell.imageView.image = [UIImage imageNamed:@"default.jpg"];
    cell.textLabel.text = [self._dataList objectAtIndex:[indexPath row]];
    cell.detailTextLabel.text = @"详细信息";
    
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self._dataList count];
}


#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *value = [__dataList objectAtIndex:[indexPath row]];
    
    printf("value = %s", [value UTF8String]);
}

@end

 

UITableView(二)

标签:

原文地址:http://www.cnblogs.com/Fredric-2013/p/5930061.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!