我想了解iOS開發,並遵循Apple自己的教程來製作待辦事項列表。在教程後,他們建議我繼續構建應用程序,實現更多功能等等。點擊展開並在UITableViewCell中顯示更多文本
我想實現的是當用戶點擊一個單元格時,它展開並顯示額外的內容。例如:現在
Buy groceries <- "item1"
Buy apples <- "subitem1"
Buy bread <- "subitem2"
Buy milk <- "subitem3"
Do homework <- "item2"
Go for a jog <- "item3"
我有一個XYZToDoSubItem
類和XYZToDoItem
類:
Buy groceries <- "item1"
Do homework <- "item2"
Go for a jog <- "item3"
挖掘時,打開這個。 XYZToDoItem
從XYZToDoSubItem
繼承名稱,完成狀態和創建日期。另外它有一個包含子項目的可變數組。
我也實施一組的其它功能:
- 當用戶「擁有」細胞進入編輯模式
- 當用戶雙抽頭的細胞,將其設置完成狀態爲YES和添加複選標記
所以單擊不能干擾這些功能。
我也有一個水龍頭一個「監聽器」(未完成viewDidLoad
功能):
-(void) viewDidLoad {
UITapGestureRecognizer *stp = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
stp.numberOfTapsRequired = 1;
stp.numberOfTouchesRequired = 1;
[stp requireGestureRecognizerToFail:dtp];
[self.tableView addGestureRecognizer:stp];
}
凡dtp
是雙擊識別。
我handleSingleTap
功能如下:
- (void)handleSingleTap:(UITapGestureRecognizer *)gestureRecognizer {
CGPoint p = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];
if(indexPath != nil) {
//Add contents to cell and update height here
}
}
基本上它的工作原理是,當用戶點擊一個單元格(不進行再次挖掘它(無雙擊)),handleSingleTap
運行。在這裏,我們獲得了單元格的indexPath
,如果這不是nil
(這意味着單元被挖掘,而不是背景),我想添加更多內容到單元格並更新高度。
我該如何着手做這件事?
是否有可能只需要添加的內容爲多subtitle
s,然後調用[self.tableView realoadData]
或[self.tableView beginUpdates]
和[self.tableView endUpdates]
,和iOS處理調整自己?如果是這樣的話,我該如何添加數據?我怎樣才能調用resize方法?如果不是,插入數據後如何手動調整單元格大小?
我的設置是這樣的:
In the ViewController class:
@property NSMutableArray *toDoItems;
In the ToDoItem class (inherits from subitem):
@property NSMutableArray *toDoSubItems;
In the SubItem class:
@property NSString *itemName;
@property BOOL completed;
@property (readonly) NSDate *creationDate;
我希望有人能夠找出如何解決這個問題。
在此先感謝, 亞歷山大。