View
//回傳目前有幾個項目
- (NSInteger)numberOfSectionsInTableView:(UITableView *)TableView
{
return 2;
}
//回傳目前列表的數量
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger n =0;
switch (section) {
case 0:
n=[list count]; //陣列的數量
break;
case 1:
n=[list2 count];
break;
}
return n;
}
//顯示列表
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *indicator=@"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indicator];
if(cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indicator];
}
switch (indexPath.section) {
case 0:
cell.textLabel.text=[list objectAtIndex:indexPath.row];
break;
case 1:
cell.textLabel.text=[list2 objectAtIndex:indexPath.row];
break;
}
return cell;
}
//顯示列表上方的標題
-(NSString*) tableView:(UITableView*) tableView titleForHeaderInSection:(NSInteger)section
{
NSString * header= @"";
switch (section) {
case 0:
header=@"標題1";
break;
case 1:
header=@"標題2";
break;
}
return header;
}
Select
//點選資料顯示
-(void)tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%@",[list objectAtIndex:indexPath.row]);
}
Delete
//回傳列表項目的按鈕是刪除
-(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
//刪除選擇的項目
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {
case 0:
[list removeObjectAtIndex:indexPath.row];
break;
case 1:
[list2 removeObjectAtIndex:indexPath.row];
break;
}
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
Add
//上方工具列的新增,工具列在拉進來此做動作,工具列不要拉到View裡面了,不然會跟著View的列表新增一直往下拉。
- (IBAction)insertData:(id)sender {
static NSInteger i;
[list addObject:[NSString stringWithFormat:@"%ld",(long)i++]];
for(UIView *view in self.view.subviews)
{
if([view isKindOfClass:[UITableView class]])
{
UITableView *t = (UITableView *)view;
[t reloadData];
break;
}
}
}
Sort
//列表最右邊可以用滑鼠拖移,點住不放即可拉至想移往的地方
//這一個function也是工具列的按鈕拉下來了,按一下編輯模式的拉開與關閉
- (IBAction)edit:(UIBarButtonItem *)sender
{
if(self.mytable.isEditing)
{
sender.title = @"Edit";
self.mytable.editing = NO;
}
else
{
sender.title = @"Done";
self.mytable.editing = YES;
}
}
//設定是不是可以移動列表
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//列表交換
-(void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
[list exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
}