iOS 自定义UItableViewCell.

1、首先新建View

2、将Table View cell 控件拖入View 

3、自定义Table View Cell 的identifier 

4、设计自己的Table View Cell

5、在UItableView 方法中声明使用自己自定义的Cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
   //CustomCellIdentifier 为自己自定义的cell identifier
    static BOOL nibsRegistered = NO;
    if (!nibsRegistered) {
        
        UINib *nib;        
        if ([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPad) {
            nib=[UINib nibWithNibName:@"UIDownLoadCell_ipad" bundle:nil];
        }else{
            nib=[UINib nibWithNibName:@"UIDownLoadCell_iphone" bundle:nil];
        }
    [tableView registerNib:nib forCellReuseIdentifier:CustomCellIdentifier];        
        nibsRegistered = YES;
    }
    
    UIDownLoadCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];  
//分别设置自己在Cell 中添加的控件。 
    [cell.areaname setText:[self.areanames objectAtIndex:indexPath.row]];
    [cell.progress setHidden:YES ];
    NSString * url=[NSString stringWithFormat:@"%@%@",hostUrl,[self.downLoadUrl objectAtIndex:indexPath.row]];
    cell.downloader=[[DWDownloader alloc]initWithPerpeties:url localFile:[self.downLoadUrl objectAtIndex:indexPath.row] threadCount:1];
    [cell.downLoad addTarget:cell action:@selector(startDownLoad:) forControlEvents:UIControlEventTouchUpInside];    
    [self.allCells setObject:cell forKey:[self.downLoadUrl objectAtIndex:indexPath.row]];
    
    if ([cell.areaname.text hasPrefix:@"浙江省"]&&[cell.areaname.text hasSuffix:@")"]) {
        [cell.downLoad setBackgroundImage:self.hasDownLoadImg forState:UIControlStateNormal];
        [cell.downLoad setEnabled:NO];
    }else if(![cell.areaname.text hasPrefix:@"浙江省"]&&[cell.areaname.text hasSuffix:@")"]){
        [cell.downLoad setBackgroundImage:self.deleteImgImg forState:UIControlStateNormal];    
    }
    return cell;
}

相关推荐