2013-02-26 90 views
1

我一直在網上尋找答案,我相信它有一個簡單的答案。自定義「浮動」UITableViewCells

我在我的應用程序中創建了UITableView,我希望它具有「浮動」表格視圖單元格和頂部菜單。就像這樣:

enter image description here

我相信,這些都是自定義的UITableView細胞,但我不知道如何像這樣創造他們,讓他們根據內容的大小動態的,如何將一個菜單一旦用戶向下滾動或向上滾動,頂部消失/顯示。

對此的任何見解都會很棒!

+1

我不認爲他們的UITableView細胞......我認爲他們只是嵌套在一個UIScrollView內UIViews。我不認爲它與UITableView有什麼關係......相反,可能有一個for循環只是產生一堆vies並相應地將它們分隔開來。 – 2013-02-26 02:58:41

+0

你是什麼意思浮動?你的意思是這個「外觀」,還是你在談論細胞的某種行爲。 – rdelmar 2013-02-26 02:58:54

+0

菜單看起來像一個自定義的UIView/UIViewController,而主區域看起來像一個帶有自定義的UICollectionViewCells的UICollectionView。我會以谷歌爲例,基於這些類型的代碼。 – 2013-02-26 02:59:22

回答

4

這可以通過分組表格視圖中的子類UITableViewCell輕鬆完成。下面的圖片顯示了我通過拖拽各種UI元素,以及創建一個自定義類,該類只有.h文件中的IBOutlets。

enter image description here

與亂碼在它被綁定到的下方,到該單元的頂部的灰色視圖,沒有特定的高度集中的標籤,因此,當細胞的增長,它將會增加。下面是我用來填充表的代碼:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.theData = @[@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven",@"Eight",@"Nine"]; 
    [self.tableView reloadData]; 
} 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return self.theData.count; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 1; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *s = @"asdfhfl fl flfh sflhsalfjh fajlhf lf asldf fh asljfafh sjlfh ajf fljf fasjlfhjfhjfhjsf hsjfhsjfhajsfh the end"; 
    CGSize size = [s sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(281, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping]; 
    return size.height + 130; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    RDCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RDCell" forIndexPath:indexPath]; 
    cell.lbl1.text = self.theData[indexPath.section]; 
    cell.lbl2.text = @"asdfhfl fl flfh sflhsalfjh fajlhf lf asldf fh asljfafh sjlfh ajf fljf fasjlfhjfhjfhjsf hsjfhsjfhajsfhajlfjafh"; 
    return cell; 
} 

請注意,我部分的數量設置爲陣列的數量,這樣就可以獲得每1排的獨立的部分。 heightForRowAtIndexPath中的代碼是您計算單元格高度的典型方式(除了通常使用索引路徑併爲每個單元格獲取不同的字符串)。

相關問題