IOS学习 UITableView 单元格风格和修改单元格背景

来源:互联网 发布:勒索软件下载 编辑:程序博客网 时间:2024/05/03 10:42

@implementation HomeViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    self.title =@"UITabelViewCell";

    

    _listArray =@[@"单元格样式",@"定制单元格背景"];

    

    _tableView = [[UITableViewalloc]initWithFrame:self.view.boundsstyle:UITableViewStylePlain];

    [self.viewaddSubview:_tableView];

    

    //设置数据源

    _tableView.dataSource =self;

    //设置委托

    _tableView.delegate =self;

}


- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    //获得当前表视图选中的单元格

    NSIndexPath *indexPath = [_tableViewindexPathForSelectedRow];

    //取消当前表视图的选中状态

    [_tableViewdeselectRowAtIndexPath:indexPath animated:YES];

}


#pragma mark - UITableView DataSource

//表视图中存在section的个数,默认为1

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;

}


//section中包含row的数量

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return_listArray.count;

}


//创建单元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *cellIndentifier =@"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];

    if (cell == nil) {

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIndentifier];

    }

    cell.textLabel.text =_listArray[indexPath.row];

    return cell;

}


#pragma mark - UITableView delegate

//当用户选择某一行时

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    

    DetaiViewController *detaiVC = [[DetaiViewControlleralloc]init];

    //获取选择的内容

    detaiVC.isBaseCell = indexPath.row ?NO : YES ;

    [self.navigationControllerpushViewController:detaiVC animated:YES];

}




#import <UIKit/UIKit.h>

#import "HomeViewController.h"


@interface DetaiViewController :UIViewController<UITableViewDelegate,UITableViewDataSource>{

    NSArray *array;

    NSMutableArray *tempArray;

    NSMutableArray *fontArray;

    UITableViewStyle style;

    UITableViewCell *cell;

}


@property (nonatomic,assign)BOOL isBaseCell;


@end






@implementation DetaiViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

  

    array = [UIFontfamilyNames];

    if (!self.isBaseCell)

    {

        self.title =@"Modify Cell BG Type";

        style =UITableViewStyleGrouped;

        fontArray = [[NSMutableArrayalloc]init];

        for (int i =0; i<array.count; i++)

        {

            //取出字体

            NSString *font = array[i];

            //5整除的时候,创建tempArray数据,添加至fontArray

            if (i %5 ==0) {

                tempArray = [[NSMutableArrayalloc]init];

                [fontArray addObject:tempArray];

            }

            [tempArray addObject:font];

        }

    }else

    {

        self.title =@"Cell Base Type";

        style =UITableViewStylePlain;

    

    }

    UITableView *detaiTV = [[UITableViewalloc]initWithFrame:self.view.boundsstyle:style];

    [self.viewaddSubview:detaiTV];

    

        detaiTV.delegate = self;

        detaiTV.dataSource = self;

}


#pragma mark - UITableView DataSource

//表视图中存在section的个数,默认为1

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    if (_isBaseCell) {

        return 1;  //选中第一行时

    }else{

        return fontArray.count;}

}

//section中包含row的数量

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (_isBaseCell) {

        return 4;

    }else{

        return [fontArray[section]count];}

}

//创建单元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (_isBaseCell) {

        //只有一页,无需重用

        cell = [[UITableViewCellalloc]initWithStyle:indexPath.rowreuseIdentifier:nil];

        /* 单元格风格  Style : indexPath.row

         * 0   UITableViewCellStyleDefault

         * 1   UITableViewCellStyleValue1

         * 2   UITableViewCellStyleValue2

         * 3   UITableViewCellStyleSubtitle  */

        switch (indexPath.row) {

            case 0:

                cell.textLabel.text =@"这是一个默认的风格";

                cell.detailTextLabel.text =@"这是副标题";//不显示

                cell.imageView.image = [UIImageimageNamed:@"photo"];

                cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;  //箭头

                break;

            case 1:

                cell.textLabel.text =@"这是一个Value1的风格";

                cell.detailTextLabel.text =@"这是副标题";

                cell.imageView.image = [UIImageimageNamed:@"photo"];

                cell.accessoryType =UITableViewCellAccessoryCheckmark; //打勾

                break;

            case 2:

                cell.textLabel.text =@"这是一个Value2的风格";

                cell.detailTextLabel.text =@"这是副标题";

                cell.imageView.image = [UIImageimageNamed:@"photo"];  //不显示

                cell.accessoryType =UITableViewCellAccessoryDetailDisclosureButton//按钮+箭头

                break;

            case 3:

                cell.textLabel.text =@"这是一个subTitle的风格"//副标题在下方

                cell.detailTextLabel.text =@"这是副标题";

                cell.imageView.image = [UIImageimageNamed:@"photo"];

                cell.accessoryType =UITableViewCellAccessoryDetailButton;//按钮

                break;

            default:

                break;

        }

    }else

    {

        static NSString *cellIndentifier =@"cell";

        cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];

        if (cell ==nil)

        {

            cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIndentifier];

        }

    

        cell.textLabel.text = [[fontArrayobjectAtIndex:indexPath.section]objectAtIndex:indexPath.row];

        //设置选中背景的风格

//        cell.selectionStyle = UITableViewCellSelectionStyleBlue;

        //设置背景图片

        UIImageView *imageView = [[UIImageViewalloc]init];

        imageView.image = [UIImageimageNamed:@"bg_balance"];

        cell.backgroundView = imageView;

        

        UIImageView *selectionView = [[UIImageViewalloc]init];

        selectionView.backgroundColor = [UIColoryellowColor];

        cell.selectedBackgroundView = selectionView;        

    }

    return cell;

}


//设置section头部视图的title  此方法与自定义的头部视图不能并存

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    if (_isBaseCell) {

        return nil;

    }else{

        NSString  *title = [NSStringstringWithFormat:@"%ldsection",(long)section+1];

        return title;}

}


#pragma mark - UITableView Delegate

//设置section头部的高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    if (_isBaseCell) {

        return 0;

    }return 25;

}



0 0