2011-04-11 54 views
4

我正在構建具有表格視圖的應用程序。但我希望這是一個表格視圖,當你點擊它時展開單元格,當你第二次點擊時關閉。點擊時更改UITableView中的單元格內容

但我想知道以下是否可能。當單元格未被選中時,您只能看到圖片,標題和文本的開頭。但是一旦選中了單元格,它將展開並顯示更多的子視圖,即圖像視圖。

這可能嗎?例如,要隱藏單元格中的子視圖,只要它被輕敲,它就可以看到並按正確的方式對齊?當然,我該怎麼做?

Thnx !!!

回答

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 
+0

謝謝!這肯定會幫助 – Jos 2011-04-11 17:20:28

+0

不要猶豫,投票! – vikingosegundo 2011-04-12 17:25:45

+0

使用你的代碼,像魅力一樣工作! – Jos 2011-04-14 13:37:41

4

是的,我在現在正在處理的應用程序中執行此操作。

您需要跟蹤單元格處於打開狀態或關閉狀態。如果一次只能打開一個單元格,可以通過保持對當前indexPath的引用來完成。如果多個單元格可以同時打開,則需要一個布爾值數組,用於跟蹤每個單元格是打開還是關閉。

在heightForRowAtIndexPath中,根據行是打開還是關閉返回正確的高度。

在cellForRowAtIndexPath中,如果該行已關閉,則隱藏關閉時應該看不到的所有內容。意見仍然可以在那裏,但他們應該被設置爲hidden = YES

最後,在didSelectRowAtIndexPath中,將給定的索引路徑設置爲在打開時打開,如果打開則關閉,然後使用[tableView reloadRowsAtIndexPaths:]重新加載單元格。如果您一次只允許打開1個,那麼只需將當前打開的索引路徑設置爲所選的打開索引路徑,然後重新加載已選擇的路徑和以前打開的路徑。

+0

偉大的答案,會嘗試這馬上! – Jos 2011-04-11 17:19:53

+0

嘿Gendolkari,我不太明白我需要如何設置隱藏狀態。我只需要用戶選擇,即如果(cellState == @「關閉){secondLabel.hidden = YES} else {secondLabel.hidden = NO}?DoFor't cellForRowAtIndexPath只被調用一次,因爲它使用reuseidentifier?Thnks! – Jos 2011-04-14 13:39:18

+0

是的,只要設置secondLabel.hidden = YES。不要用「==」來比較2個字符串;你需要使用'isEqualToString:'方法'cellForRowAtIndexPath:'每次單元被重載或顯示時被調用在屏幕上顯示,重用標識符只是用來停止再次調用[UITableViewCell alloc];但是'cellForRowAtIndexPath:'方法被調用。 – GendoIkari 2011-04-14 14:30:39

相關問題