在UITableView中扩展一行

时间:2012-01-17 08:48:57

标签: iphone uitableview row

我有一个有六行的UITableView。每行左侧有一个“+”符号。单击该按钮,将显示详细信息。例如,数据的描述。

再次点击它,细节应该消失。我该如何实现呢?

请帮帮我。

提前致谢。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:-1)

如果您只能为iOS5构建应用,请查看WWDC 2011 Session 125视频

UITableView更改,提示&amp;花样 https://developer.apple.com/videos/wwdc/2011/

它解释了如何做到这一点。如果你必须支持旧版本,它至少会给你一个想法!