搜尋此網誌

2012年8月28日 星期二

客制化UITableViewCell


CustomerCell.h
#import <UIKit/UIKit.h>

@interface CustomerCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UIImageView *filmImageView;
@property (strong, nonatomic) IBOutlet UILabel *filmNameLabel;
@property (strong, nonatomic) IBOutlet UILabel *instructorLabel;
@property (strong, nonatomic) IBOutlet UILabel *pointLabel;

@end

.m不變



controllerView.m
#import "CustomerCell.h"

// 回傳某個路徑下的表格單元
- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"CustomerCell";
    static BOOL nibsRegistered = NO;
    if (!nibsRegistered) {
        UINib *nib = [UINib nibWithNibName:@"CustomerCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:CellIdentifier];
        nibsRegistered = YES;
    }

    CustomerCell *cell = (CustomerCell *)[tableView 
                             dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CustomerCell alloc
                initWithStyle:UITableViewCellStyleSubtitle  reuseIdentifier:CellIdentifier];
    }
   
    if(indexPath.row<[videoList count]){
        film=[videoList objectAtIndex:indexPath.row];
        cell.filmNameLabel.text=film.filmName;
        cell.instructorLabel.text=film.instructor;
        cell.pointLabel.text=[[NSString alloc] initWithFormat: @"點數:%d",film.point];
        
        MyManager *sharedManager = [MyManager sharedManager];

        //此處改為用AFNetworking
        NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@/%@",sharedManager.apiUrl, film.filmImage]];
        cell.filmImageView.hidden=NO;
        [cell.filmImageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder"]];

    }else {
        cell.filmNameLabel.text=@"顯示更多影片";
        cell.instructorLabel.text=@"";
        cell.pointLabel.text=@"";
        cell.filmImageView.hidden=YES;
    }
    return cell;
}

沒有留言:

張貼留言