我想展開單元格,當選中時顯示一排按鈕,如上圖所示。我有一個xib
文件,它顯示了一個320寬x 140高的單元。我已經劃分了UITableViewCell
。此外,我還有另一個xib
文件,其中有一排按鈕,如上圖中的藍色墨水所示。
我能夠加載使用這個答案from a really smart guy使用initWithCoder
按鈕的排!
然而,它覆蓋MyCustomCell在我的所有其它視圖。
只是當細胞膨脹,導致它被放置在140pt高細胞的下半部我怎樣才能加載XIB?
我想展開單元格,當選中時顯示一排按鈕,如上圖所示。我有一個xib
文件,它顯示了一個320寬x 140高的單元。我已經劃分了UITableViewCell
。此外,我還有另一個xib
文件,其中有一排按鈕,如上圖中的藍色墨水所示。
我能夠加載使用這個答案from a really smart guy使用initWithCoder
按鈕的排!
然而,它覆蓋MyCustomCell在我的所有其它視圖。
只是當細胞膨脹,導致它被放置在140pt高細胞的下半部我怎樣才能加載XIB?
做了進一步的研究之後,我發現了一個不同的方式做一些像你想只用一個單元的內容。在這種方法中,您只有較高的單元格,但返回的高度只能讓您看到單元格的上半部分,除非它被選中。你必須在你的單元設計中做兩件事來完成這項工作。首先在IB中選擇「剪輯子視圖」框,以便按鈕不會顯示在視圖外(單元格)。其次,製作約束條件,使按鈕全部相互垂直排列,併爲其中一個垂直間距約束指向單元格頂部的UI元素之一。確保沒有任何按鈕對單元格底部有約束。通過這樣做,按鈕將與頂部元素(應該有一個固定的空間到單元格的頂部)保持一個固定的距離,這將導致它們在單元格短時隱藏。要對高度變化進行動畫處理,您只需調用beginUpdates,然後在表視圖上調用endUpdates。
#import "TableController.h"
#import "TallCell.h"
@interface TableController()
@property (strong,nonatomic) NSArray *theData;
@property (strong,nonatomic) NSIndexPath *selectedPath;
@end
@implementation TableController
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"TallCell" bundle:nil] forCellReuseIdentifier:@"TallCell"];
self.theData = @[@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven",@"Eight",@"Nine"];
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.theData.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath isEqual:self.selectedPath]) {
return 88;
}else{
return 44;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TallCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TallCell" forIndexPath:indexPath];
cell.label.text = self.theData[indexPath.row];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.selectedPath isEqual:indexPath]) {
self.selectedPath = nil;
}else{
self.selectedPath = indexPath;
}
[tableView beginUpdates];
[tableView endUpdates];
}
此,我想給你當你選擇第一個單元格中所需的動畫,但你得到一個稍微不同的動畫,如果你再挑當前選定一個的下面的電池。我不知道有什麼方法。
我不確定你在兩個xib文件中有什麼,但我會這樣做的方式是爲較短的單元格創建一個xib文件,爲較高的單元格創建一個xib文件(其中包含所有內容在你的圖中顯示)。我這樣做是這樣的,有兩個XIB文件,就像我上面提到:
#import "TableController.h"
#import "ShortCell.h"
#import "TallCell.h"
@interface TableController()
@property (strong,nonatomic) NSArray *theData;
@property (strong,nonatomic) NSIndexPath *selectedPath;
@end
@implementation TableController
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"ShortCell" bundle:nil] forCellReuseIdentifier:@"ShortCell"];
[self.tableView registerNib:[UINib nibWithNibName:@"TallCell" bundle:nil] forCellReuseIdentifier:@"TallCell"];
self.theData = @[@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven",@"Eight",@"Nine"];
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.theData.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath isEqual:self.selectedPath]) {
return 88;
}else{
return 44;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (! [self.selectedPath isEqual:indexPath]) {
NSLog(@"returning short");
ShortCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShortCell" forIndexPath:indexPath];
cell.label.text = self.theData[indexPath.row];
return cell;
}else{
NSLog(@"returning long");
TallCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TallCell" forIndexPath:indexPath];
cell.label.text = self.theData[indexPath.row];
return cell;
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([self.selectedPath isEqual:indexPath]) {
self.selectedPath = nil;
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
return;
}
NSIndexPath *lastSelectedPath = self.selectedPath;
self.selectedPath = indexPath;
if (lastSelectedPath != nil) {
[self.tableView reloadRowsAtIndexPaths:@[indexPath,lastSelectedPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}else{
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
我想這和它的作品。但是,當我再次點擊selectedCell時,它會崩潰應用程序。有任何想法嗎?另外,我注意到它並沒有完全執行像手風琴一樣展開單元格以顯示按鈕行的期望行爲。它有點淡入新的TallCell而不是ShortCell。任何想法如何創造這種效果? – thewheelz
@AlanMond,對不起,我忘記考慮選擇已擴展的同一行。我編輯了我的答案來解決這個問題。我不知道一種展示底部擴張的方法。 – rdelmar
是的,修正了它!你能解釋一下[self.tableview reloadRowsAtIndexPaths:@ [indexPath,lastSelectedPath] ...我不明白lastSelectedPath如何使高細胞出現,並隱藏短信元。 – thewheelz
我到處尋找如何剪輯子視圖。感謝您在解釋中包含這一點!我仍然試圖通過擴展單元格高度來完成這項工作,並仍然讓單元格像上面的單元格一樣工作。 – thewheelz
我發現解決它的一種方法是使用reloadRowsAtIndexpath方法。不幸的是,這也更新了單元格。我想知道我們是否可以重新加載它的beginupdate部分而不會丟失行數據.... – thewheelz