0

I have a UITableView with six rows. There is a "+" sign at the left of each row. On clicking that, details should appear. For example a description of the data.

On clicking it again, the detail should vanish. How can I implement this?

Please help me.

Thanks in advance.

Max MacLeod
  • 25,335
  • 12
  • 98
  • 129
user1001011
  • 41
  • 3
  • 8

2 Answers2

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
amgalanBE
  • 51
  • 3
-1

if you can build your app for iOS5 only, check out the WWDC 2011 Session 125 Video

UITableView Changes, Tips & Tricks https://developer.apple.com/videos/wwdc/2011/

It explains how it can be done. If you have to support older versions it will at least give you an idea!

thumbsup
  • 429
  • 5
  • 11