我正在构建具有表格视图的应用程序。但我希望这是一个表格视图,当你点击它时展开单元格,当你第二次点击时关闭。点击时更改UITableView中的单元格内容
但我想知道以下是否可能。当单元格未被选中时,您只能看到图片,标题和文本的开头。但是一旦选中了单元格,它将展开并显示更多的子视图,即图像视图。
这可能吗?例如,要隐藏单元格中的子视图,只要它被轻敲,它就可以看到并按正确的方式对齐?当然,我该怎么做?
Thnx !!!
我正在构建具有表格视图的应用程序。但我希望这是一个表格视图,当你点击它时展开单元格,当你第二次点击时关闭。点击时更改UITableView中的单元格内容
但我想知道以下是否可能。当单元格未被选中时,您只能看到图片,标题和文本的开头。但是一旦选中了单元格,它将展开并显示更多的子视图,即图像视图。
这可能吗?例如,要隐藏单元格中的子视图,只要它被轻敲,它就可以看到并按正确的方式对齐?当然,我该怎么做?
Thnx !!!
我在前段时间做过类似的事情。你会在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
是的,我在现在正在处理的应用程序中执行此操作。
您需要跟踪单元格处于打开状态或关闭状态。如果一次只能打开一个单元格,可以通过保持对当前indexPath的引用来完成。如果多个单元格可以同时打开,则需要一个布尔值数组,用于跟踪每个单元格是打开还是关闭。
在heightForRowAtIndexPath中,根据行是打开还是关闭返回正确的高度。
在cellForRowAtIndexPath中,如果该行已关闭,则隐藏关闭时应该看不到的所有内容。意见仍然可以在那里,但他们应该被设置为hidden = YES
。
最后,在didSelectRowAtIndexPath中,将给定的索引路径设置为在打开时打开,如果打开则关闭,然后使用[tableView reloadRowsAtIndexPaths:]
重新加载单元格。如果您一次只允许打开1个,那么只需将当前打开的索引路径设置为所选的打开索引路径,然后重新加载已选择的路径和以前打开的路径。
伟大的答案,会尝试这马上! – Jos 2011-04-11 17:19:53
嘿Gendolkari,我不太明白我需要如何设置隐藏状态。我只需要用户选择,即如果(cellState == @“关闭){secondLabel.hidden = YES} else {secondLabel.hidden = NO}?DoFor't cellForRowAtIndexPath只被调用一次,因为它使用reuseidentifier?Thnks! – Jos 2011-04-14 13:39:18
是的,只要设置secondLabel.hidden = YES。不要用“==”来比较2个字符串;你需要使用'isEqualToString:'方法'cellForRowAtIndexPath:'每次单元被重载或显示时被调用在屏幕上显示,重用标识符只是用来停止再次调用[UITableViewCell alloc];但是'cellForRowAtIndexPath:'方法被调用。 – GendoIkari 2011-04-14 14:30:39
谢谢!这肯定会帮助 – Jos 2011-04-11 17:20:28
不要犹豫,投票! – vikingosegundo 2011-04-12 17:25:45
使用你的代码,像魅力一样工作! – Jos 2011-04-14 13:37:41