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.myTableViewsetEditing:YESanimated: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;
}