@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