如果對(duì)
UITableView的相關(guān)屬性和協(xié)議還存在疑問(wèn),請(qǐng)先閱讀我上一篇文章 iOS表視圖UITableView之基礎(chǔ)篇(一)。
UITableViewController
- 特點(diǎn):
UITableViewController繼承于UIViewController,自帶一個(gè)tableView。
- 在
UITableViewController中,self.view和self.tableView是同一個(gè)對(duì)象。 -
datasource和delegate默認(rèn)都是self(UITableViewController) - 開(kāi)發(fā)中只需要建立
UITableViewController類(lèi)
UITableView編輯
首先,在ViewController.m寫(xiě)出它的Extension(延展),方便下面代碼的使用:
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) UITableView *tableView; /** 表視圖 */
@property (nonatomic, retain) NSMutableArray *dataSource; /** 數(shù)據(jù)源數(shù)組 */
@property (nonatomic, retain) NSMutableArray *deleteIndexPathsArray; /** 將要?jiǎng)h除的元素的indexPath */
@end
-
UITableView編輯步驟如下:
一、讓UITableView處于編輯狀態(tài):
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
//先執(zhí)行父類(lèi)的setEditing方法:
[super setEditing:editing animated:animated];
//(1)讓tableView處于可編輯狀態(tài):
[self.tableView setEditing:editing animated:animated];
}
二、協(xié)議設(shè)定:
1.確定Cell是否處于編輯狀態(tài):
- (BOOL)tableView:(UITableView )tableView canEditRowAtIndexPath:(NSIndexPath )indexPath
{
//根據(jù)數(shù)據(jù)源進(jìn)行篩選:
NSString name = [[self.dataSource objectAtIndex:indexPath.row] valueForKey:@"name"];
if ([name isEqualToString:@"張偉"]) {
return NO; //如果有名字叫張偉的同學(xué),那個(gè)cell就不可編輯
}
return YES; //(2)所有都可以編輯
}
2.設(shè)定Cell的編輯樣式(刪除/添加):
- (UITableViewCellEditingStyle)tableView:(UITableView )tableView editingStyleForRowAtIndexPath:(NSIndexPath )indexPath
{
//(3)設(shè)置支持刪除操作:
// return UITableViewCellEditingStyleDelete;
//設(shè)置插入操作:
return UITableViewCellEditingStyleInsert;
}
3.編輯狀態(tài)進(jìn)行提交*:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//判斷編輯的樣式:
if (editingStyle == UITableViewCellEditingStyleDelete) {
//刪除對(duì)應(yīng)的元素:
[self.dataSource removeObjectAtIndex:indexPath.row];
//刷新tableView:
[tableView reloadData];
}
//判斷是插入操作:
if (editingStyle == UITableViewCellEditingStyleInsert) {
//設(shè)置要插入的數(shù)據(jù):
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"dog", @"name", nil];
//將字典插入到數(shù)組的指定位置:
[self.dataSource insertObject:dic atIndex:indexPath.row];
//更新視圖:
[tableView reloadData];
}
}
*** 注意:***
編輯結(jié)束后,由于numberOfRowInSection這個(gè)協(xié)議只在tableview添加到父視圖的時(shí)候走一次, 且table上的數(shù)據(jù)都是由數(shù)組提供,因此,需要先將數(shù)組中的元素刪除,然后讓table的協(xié)議重新走一遍進(jìn)行重新賦值。 即:先修改數(shù)據(jù)源,再刷新table(如上,使用reloadData方法) 。
移動(dòng)
cell的位置:
1.實(shí)現(xiàn)delegate協(xié)議,告訴tableView是否能移動(dòng):
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
2.移動(dòng):
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//記錄原來(lái)的下標(biāo)和新的下標(biāo):
NSInteger fromIndex = sourceIndexPath.row;
NSInteger toIndex = destinationIndexPath.row;
//記錄數(shù)組元素:
NSDictionary *dic = [[self.dataSource objectAtIndex:fromIndex] retain];
//從數(shù)組中刪除該元素:
[self.dataSource removeObject:dic];
//插入到新的位置:
[self.dataSource insertObject:dic atIndex:toIndex];
//更新視圖:
[tableView reloadData];
}-
實(shí)現(xiàn)
dataSource協(xié)議方法:#pragma mark --cell行數(shù): - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataSource.count; } #pragma mark --cell的內(nèi)容: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"]; cell.textLabel.text = [[self.dataSource objectAtIndex:indexPath.row] valueForKey:@"name"]; //判斷是否正在被選中:(防止重用) if (![self.deleteIndexPathsArray containsObject:indexPath]) { cell.accessoryType = UITableViewCellAccessoryNone; } else { cell.accessoryType = UITableViewCellAccessoryCheckmark; } return cell; } #pragma mark --cell的高度: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 80; } #pragma mark --cell的點(diǎn)擊方法: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //根據(jù)indexPath找到對(duì)應(yīng)的cell: UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //判斷數(shù)組里是否包含該indexPath: if ([self.deleteIndexPathsArray containsObject:indexPath]) { [self.deleteIndexPathsArray removeObject:indexPath]; //取消點(diǎn)擊狀態(tài): cell.accessoryType = UITableViewCellAccessoryNone; } else { [self.deleteIndexPathsArray addObject:indexPath]; cell.accessoryType = UITableViewCellAccessoryCheckmark; } }
以上內(nèi)容是筆者對(duì)
UITableView基礎(chǔ)的總結(jié)。由于筆者也是iOS初學(xué)者,總結(jié)過(guò)程中難免出現(xiàn)紕漏。如發(fā)現(xiàn)不足或錯(cuò)誤,歡迎批評(píng)指正。大家共同學(xué)習(xí)!共同進(jìn)步!
有關(guān)UITableView和iOS的更多知識(shí),請(qǐng)關(guān)注小編,期待后續(xù)文章!