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

时间:2011-04-11 16:50:40

标签: ios objective-c iphone uitableview cocoa-touch

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

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

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

日Thnx !!!

2 个答案:

答案 0 :(得分:6)

我前几天做过类似的事情。您可以在github找到代码。

请注意,它非常粗糙,因为它是iPhone开始的日子,即属性缺失。

·H

#import <UIKit/UIKit.h>


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

@end

的.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

答案 1 :(得分:4)

是的,我在我正在处理的应用程序中执行此操作。

您需要跟踪单元格所处的状态,无论是打开还是关闭。如果一次只能打开一个单元格,则只需保留对当前indexPath的引用即可。如果可以同时打开多个单元格,则需要一组布尔值,用于跟踪每个单元格是打开还是关闭。

在heightForRowAtIndexPath中,只需根据行是打开还是关闭返回正确的高度。

在cellForRowAtIndexPath中,如果该行已关闭,则隐藏关闭时不应显示的所有内容。视图仍然可以存在,但应将其设置为hidden = YES

最后,在didSelectRowAtIndexPath中,将给定索引路径设置为打开(如果已关闭),如果已打开则关闭,然后使用[tableView reloadRowsAtIndexPaths:]重新加载单元格。如果您一次只允许打开1个,那么只需将当前打开的索引路径设置为所选的路径,然后重新加载已选择的路径以及之前打开的路径。