点击时更改UITableView中的单元格内容

Jos*_*Jos 4 iphone cocoa-touch objective-c uitableview ios

我正在构建一个具有表格视图的应用程序.但我希望这是一个桌面视图,当你点击它时会扩展单元格,当你再次点击时它会关闭.

但我想知道以下是否可行.未选择单元格时,您只能看到图片,标题和文本的开头.但是一旦选择了单元格,它就会扩展并显示更多的子视图,即图像视图.

这可能吗?例如,要隐藏单元格中的子视图,并且一旦它被点击它就可以看到并以正确的方式对齐?当然,我该怎么做?

日Thnx !!!

Vik*_*ica 6

不久前我做了类似的事情.你会在github找到代码.

请注意,它非常粗糙,因为它是我开始的iPhone时代,即属性缺失.

.H

#import <UIKit/UIKit.h>


@interface FirstViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
    NSIndexPath *selectedIndexPath;
    NSDictionary *articles;
}

@end
Run Code Online (Sandbox Code Playgroud)

.M

#import "FirstViewController.h"
@implementation FirstViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    selectedIndexPath = nil;
    articles = [[NSDictionary dictionaryWithObject:[NSArray arrayWithObjects:@"one", @"two", @"three",
                                                    @"four", @"five", @"six",
                                                    @"seven", @"eight", @"nine",
                                                    @"ten", @"eleven", nil]
                 forKey:@"title"] retain];
}
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
- (void)dealloc {
    [selectedIndexPath release];
    [articles release];
    [super dealloc];
}

- (int)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[articles allKeys] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[articles allKeys] objectAtIndex : section];
}

- (int)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
    id key = [[articles allKeys] objectAtIndex:section];
    return [[articles objectForKey : key] count];
}

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ((selectedIndexPath != nil) && (selectedIndexPath.row == indexPath.row))
        return 80.0;
    return 40.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * MyIdentifier = @"MyIdentifier";
    UITableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
    }
    id key = [[articles allKeys] objectAtIndex:indexPath.section];
    cell.textLabel.text = [[articles objectForKey:key] objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (selectedIndexPath == indexPath) {
        selectedIndexPath = nil;
    } else {
        selectedIndexPath = indexPath;
    }
    [self.tableView deselectRowAtIndexPath : indexPath animated : NO];
    [tableView beginUpdates];
    [tableView endUpdates];
}

@end
Run Code Online (Sandbox Code Playgroud)