在找expantable listview,結果找了半天沒有找到,
原來是android叫做listview,ios則是叫做tableview!
這次在.h file完全不用作任何事情,所以就不說.h file了!
原始檔案在這邊:
http://code4app.com/ios/ExpansionTableView/5121cac66803fae949000002
是參考這個弄出來的
------------------------------------------------------------------------------------------------------------------
首先,如果你有開ARC的話,那不需要release/dealloc之類的
第一步:
在viewDidLoad裡面加入
NSString *path = [[NSBundle mainBundle] pathForResource:@"ExpansionTableTestData" ofType:@"plist"];//指定要去哪一個檔案名稱,檔案類型找,放進去path裡面
_dataList = [[NSMutableArray alloc] initWithContentsOfFile:path];
pathForResource這邊是檔案名稱
ofType則是檔案類型
所以我們要讀取的列表就是ExpansionTableTestData.plist這個檔案
顯然還沒宣告_dataList,所以在前面interface下面就要先宣告一個
NSMutableArray *_dataList;
像這樣
@interface Page3 ()<UITableViewDataSource,UITabBarDelegate>
{
NSMutableArray *_dataList;
}
@property (assign)BOOL isOpen;
@property (nonatomic,retain) NSIndexPath *selectIndex;
@property (nonatomic,retain) IBOutlet UITableView *expansionTableView;
@end
@implementation Page3
@synthesize isOpen,selectIndex;
紅色部分為新增的
再來就是新增加建構子,
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [_dataList count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.isOpen) {
if (self.selectIndex.section == section) {
return [[[_dataList objectAtIndex:section] objectForKey:@"list"] count]+1;;
}
}
return 1;
}
//iphone 5以下可以用float,5s~6 plus CGFloat
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.isOpen&&self.selectIndex.section == indexPath.section&&indexPath.row!=0) {
static NSString *CellIdentifier = @"Cell2";
Cell2 *cell = (Cell2*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
}
NSArray *list = [[_dataList objectAtIndex:self.selectIndex.section] objectForKey:@"list"];
cell.titleLabel.text = [list objectAtIndex:indexPath.row-1];
return cell;
}else
{
static NSString *CellIdentifier = @"Cell1";
Cell1 *cell = (Cell1*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
}
NSString *name = [[_dataList objectAtIndex:indexPath.section] objectForKey:@"name"];
cell.titleLabel.text = name;
[cell changeArrowWithUp:([self.selectIndex isEqual:indexPath]?YES:NO)];
return cell;
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//展開items,開始判斷式
if (indexPath.row == 0) {
//第一層
if ([indexPath isEqual:self.selectIndex]) {
self.isOpen = NO;
[self didSelectCellRowFirstDo:NO nextDo:NO];//進入下一段函式
self.selectIndex = nil;
}else
{
if (!self.selectIndex) {
self.selectIndex = indexPath;
[self didSelectCellRowFirstDo:YES nextDo:NO];
}else
{
[self didSelectCellRowFirstDo:NO nextDo:YES];
}
}
}else
{
//第二層
NSDictionary *dic = [_dataList objectAtIndex:indexPath.section];
NSArray *list = [dic objectForKey:@"list"];
NSString *item = [list objectAtIndex:indexPath.row-1];
//顯示點擊的item
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:item message:nil delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil];
[alert show];//跳出訊息
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)didSelectCellRowFirstDo:(BOOL)firstDoInsert nextDo:(BOOL)nextDoInsert
{
self.isOpen = firstDoInsert;
Cell1 *cell = (Cell1 *)[self.expansionTableView cellForRowAtIndexPath:self.selectIndex];
[cell changeArrowWithUp:firstDoInsert];//疑似把箭頭往上?
[self.expansionTableView beginUpdates];
int section = self.selectIndex.section;
int contentCount = [[[_dataList objectAtIndex:section] objectForKey:@"list"] count];
NSMutableArray* rowToInsert = [[NSMutableArray alloc] init];
for (NSUInteger i = 1; i < contentCount + 1; i++) { //第二層有幾行 = content count
NSIndexPath* indexPathToInsert = [NSIndexPath indexPathForRow:i inSection:section];
[rowToInsert addObject:indexPathToInsert];
}
if (firstDoInsert)
{ [self.expansionTableView insertRowsAtIndexPaths:rowToInsert withRowAnimation:UITableViewRowAnimationTop];
}
else
{
[self.expansionTableView deleteRowsAtIndexPaths:rowToInsert withRowAnimation:UITableViewRowAnimationTop];
}
//[rowToInsert release];
[self.expansionTableView endUpdates];
if (nextDoInsert) {
self.isOpen = YES;
self.selectIndex = [self.expansionTableView indexPathForSelectedRow];
[self didSelectCellRowFirstDo:YES nextDo:NO];
}
if (self.isOpen) [self.expansionTableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionTop animated:YES];
}
然後我們要建構xib檔,拉出一個table view (記得要選autolayer)
然後要與@property (nonatomic,retain) IBOutlet UITableView *expansionTableView;
這一行做連結(滑鼠右鍵拉過去connect)
然後還要把dataSource/delegate也連結到file's Owner
如果要修改列表的內容,就要去expansiontabletestdata.plist那邊修改喔!
完整檔案在code4app可以下載,這邊就不附上囉!
end