2014年1月7日 星期二

客製化UITableView

//在要用到的TableViewController中實作這裡
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //為某個種類的Cell給一個特定的名字作為區分
    static NSString *CellIdentifier = @"Cell";
    //如果TableView裡還有Cell的話,把cell叫出來使用
    StoresOnePlaceCell *cell = (StoresOnePlaceCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    //如果叫不出來cell,就new一個新的cell
    if (cell == nil)
    {
        cell = [[StoresOnePlaceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    // Configure the cell...
    
    [cell setCustomViewWithStoreInfo:[self.stores objectAtIndex:indexPath.row]];
    
    return cell;
}


//在你客製化的Cell中實作這二個方法,在其中引入你要客製化的View
#import "StoresOnePlaceCell.h"
#import "MapStoreView.h"

static NSInteger _kMapStoreViewTag = 7001;

@implementation StoresOnePlaceCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)setCustomViewWithStoreInfo:(NSDictionary *)storeInfo
{
    MapStoreView *storeView = (MapStoreView *)[self.contentView viewWithTag:_kMapStoreViewTag];
    BOOL _isMakeNew         = NO;
    if( !storeView )
    {
        storeView  = [MapStoreView new];
        [storeView setTag:_kMapStoreViewTag];
        _isMakeNew = YES;
    }
    storeView.people = [[storeInfo objectForKey:@"instore_account_count"] integerValue];
    storeView.name = [storeInfo objectForKey:@"name"];
    storeView.items = [[storeInfo objectForKey:@"total_items"] integerValue];
    storeView.hotness = [[storeInfo objectForKey:@"hotness"] integerValue];
    storeView.followed = [[storeInfo objectForKey:@"total_followed"] integerValue];
    storeView.distance = [[storeInfo objectForKey:@"distance"] floatValue];
    [storeView make];
    [storeView setUserInteractionEnabled:NO];
    if( _isMakeNew )
    {
        [self.contentView addSubview:storeView];
    }
}

-(CGFloat)getCustomViewHeight
{
    MapStoreView *storeView = (MapStoreView *)[self.contentView viewWithTag:_kMapStoreViewTag];
    return storeView.frame.size.height;
}


@end

沒有留言:

張貼留言