一个巧妙的自定义UITableViewCell的办法

来源:互联网 发布:mysql查询笔试题及答案 编辑:程序博客网 时间:2024/06/05 05:14

我想在UITableViewCell里增加一个switch,并且还保留原来的IMAGE,LABEL,但我又不想完全用代码实现,也不想完全用INTERFACE BUILDER画出来。

1、新建一个XIB文件,拖SWITCH放到右边

2、新建类,继承UITableViewCell,叫StatusCell

3、指定XIB的类为StatusCell

4、

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"StatusCell";
    
 
      
        // 状态的CELL
        StatusCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            
            NSBundle *myBundle = [NSBundle mainBundle];
                            cell =[[myBundle loadNibNamed:@"StatusCell" owner:self options:nil] lastObject];        }
      
        // Configure the cell...
        //cell.textLabel.text = [self.dataArray objectAtIndex:indexPath.row];
        cell.textLabel.text=[self.dataArray objectAtIndex:indexPath.row];
        cell.textLabel.backgroundColor=[UIColor clearColor];
        cell.imageView.image=[UIImage imageNamed:@"lockoverlay"];

        return cell;
        
    }