UITableView(二)

通建安
2023-12-01
#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

 

转载于:https://www.cnblogs.com/Fredric-2013/p/5930061.html

 类似资料: