2012-01-17 5 views
0

私はUITableViewに6行あります。各行の左側には「+」記号があります。それをクリックすると、詳細が表示されます。例えば、データの記述。UITableViewで行を展開する

もう一度クリックすると、詳細が消えます。これをどのように実装できますか?

私を助けてください。

ありがとうございます。

+0

http://stackoverflow.com/questions/4635338/uitableviewcell-expand-on-click – Vin

答えて

-1

あなただけiOS5をのためのアプリを構築することができれば、WWDCをチェックアウト2011セッション125ビデオ

のUITableViewの変更、ヒント&トリック https://developer.apple.com/videos/wwdc/2011/

それはそれを行うことができる方法を説明します。古いバージョンをサポートしなければならない場合は、少なくともあなたにアイデアを与えるでしょう!

1
//here is a code i made for FAQ 
//hope it answers your question 
//if you want to test it, you just need to hook up your table view to _faqTableView 
#import "ViewController.h" 

@interface ViewController() 
{ 
    NSArray *questions; 
    NSArray *answers; 
    NSMutableArray *datasource; 
    NSIndexPath *insertedLocation; 
    NSIndexPath *answerIndexPath; 
} 
-(void)deleteFrom:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath; 
-(void)insertInto:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath; 
@end 
@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    _faqTableView.dataSource = self; 
    _faqTableView.delegate = self; 

    questions = [NSArray arrayWithObjects:@"What", @"is", @"your", @"name", nil]; 
    answers = [NSArray arrayWithObjects:@"kdjfl", @"kdlfjs", @"kdljfel", @"kjldfkjsle", nil]; 

    datasource = [NSMutableArray arrayWithArray:questions]; 
    insertedLocation = [NSIndexPath indexPathForRow:[datasource count] inSection:0]; 
    answerIndexPath = insertedLocation; 
} 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [datasource count]; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"FAQ Cell Identifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (!cell) { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    if (indexPath.row != answerIndexPath.row) { 
     cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
    } else { 
     cell.textLabel.numberOfLines = 0; 
    } 
    cell.textLabel.text = [datasource objectAtIndex:indexPath.row]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    return cell; 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row != insertedLocation.row) { 
     [tableView beginUpdates]; 
     if (indexPath.row == insertedLocation.row -1 && 
      insertedLocation.row!= [datasource count]) { 
      [self deleteFrom:tableView atIndexPath:[NSIndexPath indexPathForRow:indexPath.row +1 inSection:0]]; 
      insertedLocation = [NSIndexPath indexPathForRow:[datasource count] inSection:0]; 
     } else { 
      NSInteger i = indexPath.row; 
      if (insertedLocation.row != [datasource count]) { 
       [self deleteFrom:tableView atIndexPath:insertedLocation]; 
       if (insertedLocation.row < i) { 
        i --; 
       } 
      } 
      insertedLocation = [NSIndexPath indexPathForRow:i + 1 inSection:0]; 
      [self insertInto:tableView atIndexPath:insertedLocation]; 
     } 
     [tableView endUpdates]; 
     NSLog(@"inserted row %d", insertedLocation.row); 
    } 
} 

-(void)deleteFrom:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath 
{ 
    [datasource removeObjectAtIndex:indexPath.row]; 
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
} 

-(void)insertInto:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath 
{ 
    [datasource insertObject:[answers objectAtIndex:indexPath.row - 1] atIndex:indexPath.row]; 
    [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom]; 
    answerIndexPath = indexPath; 
} 


@end