IOS 学习 TableView

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    _array = [NSArray arrayWithObjects:@”Monday”,@”Tuesday”,@”Wednesday”,@”Yhursday”,@”Friday”,@”Saturday”,@”Sunday”, nil];

    CGRect rect = CGRectMake(0, 40, 300, 420);
    UITableView *tableView = [[UITableView alloc] initWithFrame:rect];

    tableView.delegate = self;
    tableView.dataSource = self;

// tableView.separatorColor = [UIColor clearColor];

//清除分割线
[tableView setSeparatorStyle:UITableViewScrollPositionNone];

// //初始化NSIndexPath对象,指定UITableView滑动到的目标位置
// NSIndexPath *scrollTo =[NSIndexPath indexPathForRow:11 inSection:0];
// [tableView scrollToRowAtIndexPath:scrollTo atScrollPosition:UITableViewScrollPositionTop animated:NO];

[self.view addSubview:tableView];

}

//定义表格的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_array count];
}

//设置cell的高度
-(CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
{
return 80;
}

//初始化一个cell
-(UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @”cellidentifier”;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];

    NSInteger num = [indexPath row];

    cell.textLabel.text = [_array objectAtIndex:num];

// cell.textLabel.text = [NSString stringWithFormat:@”Cell item #%i”,num];
// cell.textLabel.text = @”cell cell”;
cell.detailTextLabel.text = @”Detail Information here”;

    if (num % 2 == 0) {
        [cell setBackgroundColor:[UIColor purpleColor]];
    }else{

// CGRect rect = CGRectMake(0, 0, 100, 100);
// UIView *view = [[UIView alloc] initWithFrame:rect];
// [view setBackgroundColor:[UIColor brownColor]];
// [cell setBackgroundView:view];
[cell setBackgroundColor:[UIColor brownColor]];
}

    UIImage *img1 = [UIImage imageNamed:@"navbuton"];
    UIImage *img2 = [UIImage imageNamed:@"aboutBB"];

    cell.imageView.image = img1;
    cell.imageView.highlightedImage = img2;
}
return cell;

}

//该方法用来执行cell被选中时的动作
-(void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.accessoryType == UITableViewCellAccessoryNone)
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
cell.accessoryType = UITableViewCellAccessoryNone;
}

//该代理方法用来设定CELL的编辑方式
-(UITableViewCellEditingStyle)tableView:(UITableView )tableView editingStyleForRowAtIndexPath:(NSIndexPath )indexPath
{
return UITableViewCellEditingStyleDelete;
}

//该代理方法用来处理cell编辑结束后的动作
-(void)tableView:(UITableView )tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath )indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSInteger num = [indexPath row];
[_array removeObjectAtIndex:num];
NSArray *indexPath = [NSArray arrayWithObjects:indexPath, nil];
[tableView deleteRowsAtIndexPaths:indexPath withRowAnimation:UITableViewRowAnimationAutomatic];
}
}

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

@end

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值