1:创建一个简单的TableView
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.myTableList = [[NSMutableArray alloc] initWithCapacity:0];
NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:@"00", @"11", @"22", @"33", @"44", @"55", @"66", @"77", @"88", @"99", nil];
self.myTableList = myArray;
CGRect rect = [[UIScreen mainScreen] bounds];
self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height-20) style:UITableViewStyleGrouped];
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
//改变背景颜色
self.myTableView.backgroundView = nil;
self.myTableView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:self.myTableView];
}
#pragma mark TableViewDelegate
//多少section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
//section头内容
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"";
}
//返回section中row行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.myTableList count];
}
//返回row的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44;
}
//
- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
static NSString *tableCellIdentifier1 = @"tableCellIdentifier1";
static NSString *tableCellIdentifier2 = @"tableCellIdentifier2";
NSInteger nRow = [indexPath row];
//不通过xib加载cell
if (indexPath.section == 0) {
UITableViewCell *pCell = [tableView dequeueReusableCellWithIdentifier:tableCellIdentifier1];
if (pCell == nil) {
pCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableCellIdentifier1];
}
pCell.textLabel.text = [self.myTableList objectAtIndex:nRow];
pCell.imageView.image = [UIImage imageNamed:@"test.png"];
//cell右侧的显示样式
pCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
if (nRow == 0) {
//当为UITableViewCellSelectionStyleNone点击时不显示颜色变化
pCell.selectionStyle = UITableViewCellSelectionStyleNone;
} else if (nRow == 1) {
pCell.selectionStyle = UITableViewCellSelectionStyleNone;
//改变cell背景颜色
pCell.textLabel.text = @"";
UIView *backgrdView = [[UIView alloc] initWithFrame:pCell.frame];
backgrdView.backgroundColor = [UIColor redColor];
pCell.backgroundView = backgrdView;
}
return pCell;
} else if (indexPath.section == 1) { //通过xib加载cell
OAApplyCell *pCell = [tableView dequeueReusableCellWithIdentifier:tableCellIdentifier2];
if (!pCell) {
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"OAApplyCell" owner:self options:nil];
for (NSObject *o in objects) {
if ([o isKindOfClass:[OAApplyCell class]]) {
pCell = (OAApplyCell *)o;
break;
}
}
}
pCell.titleLab.text = [self.myTableList objectAtIndex:nRow];
return pCell;
}
return nil;
}
2:获取tableView中cell
- (void)getOneCell
{
NSInteger nRow = 0;
UITableViewCell *cell = [self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:nRow inSection:1]];
OAApplyCell *pCell = (OAApplyCell *)cell;
pCell.titleLab.text = @"test";
}
3:tableView中cell向右移,非xib加载cel时 pCell.textLabel.text才起作用
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
return row;
}
4:禁止tableView中cell点击响应
//判断选中的行(阻止选中第一行)
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
if (row == 0)
return nil;
return indexPath;
}
5:滑动删除cell
//划动cell是否出现del按钮
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
//删除Cell
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger nRow = [indexPath row];
[self.myTableList removeObjectAtIndex:nRow];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
6:移动cell 这边需要设置 [ self . myTableView setEditing : YES animated : YES ]; 才可以编辑移动
//移动cell
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}
- (BOOL)tableView:(UITableView *)tableView
canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath
{
// NSUInteger fromRow = [fromIndexPath row];
// NSUInteger toRow = [toIndexPath row];
//
// id object = [[self.myTableList objectAtIndex:fromRow] retain];
// [self.myTableList removeObjectAtIndex:fromRow];
// [self.myTableList insertObject:object atIndex:toRow];
// [object release];
id object = [self.myTableList objectAtIndex:fromIndexPath.row];
[self.myTableList removeObjectAtIndex:fromIndexPath.row];
[self.myTableList insertObject:object atIndex:toIndexPath.row];
}
7:移动cell时的委托提示
//可以用来当移动某个Cell上提示
- (NSIndexPath *)tableView:(UITableView *)tableView
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
if (proposedDestinationIndexPath.section != sourceIndexPath.section)
{
//keep cell where it was...
NSLog(@"YES");
return sourceIndexPath;
} else {
//ok to move cell to proposed path...
NSLog(@"NO");
return proposedDestinationIndexPath;
}
}
8:滑动删除时防止被Delegate按钮挡住
//滑动删除,,titleLab左移,防止被delete按钮盖住。
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath*)indexPath
{
OAApplyCell * cell = (OAApplyCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.titleLab.frame = CGRectMake(30, 34, 220, 18);
}
//恢复titleLab宽度
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath*)indexPath
{
OAApplyCell * cell = (OAApplyCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.titleLab.frame = CGRectMake(30, 34, 250, 18);
}
9:设置cell右边的按钮并响应事件
//设置Cell右边按钮并响应的事件
UIImage *buttonUpImage = [UIImage imageNamed:@"01.png"];
UIImage *buttonDownImage = [UIImage imageNamed:@"02.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, 100, 40);
[button setBackgroundImage:buttonUpImage forState:UIControlStateNormal];
[button setBackgroundImage:buttonDownImage forState:UIControlStateHighlighted];
[button setTitle:@"Done" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
pCell.accessoryView = button;
10:设置编辑状态时cell内容不会缩进
//设置编辑状态时cell内容不会缩进
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}