UITableView基本使用方法

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize dataList = _dataList;
@synthesize tableView = _tableView;

-(void)viewDidLoad
{
    [super viewDidLoad];
    //初始化表格
    UITableView *view = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView = view;
    // 设置协议,意思就是UITableView类的方法交给了tabView这个对象,让完去完成表格的一些设置操作
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //把tabView添加到视图之上
    [self.view addSubview:self.tableView];
    //存放显示在单元格上的数据
    NSArray *list = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",nil];
    self.dataList = list;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //    声明静态字符串型对象,用来标记重用单元格
    static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
    //    用TableSampleIdentifier表示需要重用的单元
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             TableSampleIdentifier];
    //    如果如果没有多余单元,则需要创建新的单元
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:TableSampleIdentifier];
    }
    //    获取当前行信息值
    NSUInteger row = [indexPath row];
    //    把数组中的值赋给单元格显示出来
    cell.textLabel.text = [self.dataList objectAtIndex:row];
    //设置单元格背景颜色
    //cell.textLabel.backgroundColor = [UIColor greenColor];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    UIView *backgroundView = [[UIView alloc] initWithFrame:cell.frame];
    backgroundView.backgroundColor = [UIColor orangeColor];
    cell.backgroundView=backgroundView;
    //添加图片
    UIImage *image = [UIImage imageNamed:@"123.jpg"];
    cell.imageView.image = image;
    //被选中后高亮显示的图片1
    UIImage *highLightImage = [UIImage imageNamed:@"1.jpg"];
    cell.imageView.highlightedImage = highLightImage;
    return cell;
}

 此页面实现的是一个简易的UITableView界面,在一个页面上排列出1~20行 并且  标明数字。期间还进行了一些添加图片等一些效果展示。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值