2011-04-14 27 views
0

我想實現展開/摺疊表視圖單元格,如解釋here。我的修改是我希望這是一個完整的展開/摺疊表視圖。所以每個部分都可以摺疊/展開。我看着蘋果的例子,這似乎是一個矯枉過正的問題。凌亂UITableViewCell與展開/摺疊

我目前面臨的問題主要是由於格式和顏色問題。功能似乎很好。我希望部分標題具有白色背景,行要具有藍色背景。這裏是我的代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    if (dimension.selectedSegmentIndex == 0){ 
     if (!indexPath.row) 
      { 
       cell.textLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:18]; 
       cell.textLabel.text = [timeslot objectAtIndex:indexPath.section]; 
       if ([expandedSections containsIndex:indexPath.section]) 
       { 
        cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp]; 
       } 
       else 
       { 
        cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown]; 
       } 
      } 
      else 
      { 
       // all other rows 
       cell.contentView.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1]; 
       cell.textLabel.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1]; 
       cell.textLabel.font = [UIFont fontWithName:@"Arial" size:14]; 
       cell.textLabel.textColor = [UIColor whiteColor]; 
       NSArray *listItems = [[timeslot objectAtIndex:indexPath.section] componentsSeparatedByString:@":"]; 
       if (indexPath.row == 1){ 
        cell.textLabel.text = [NSString stringWithFormat:@"%@:00 - %@:15", [listItems objectAtIndex:0], [listItems objectAtIndex:0]]; 
       } 
       if (indexPath.row == 2){ 
       cell.textLabel.text = [NSString stringWithFormat:@"%@:15 - %@:30", [listItems objectAtIndex:0], [listItems objectAtIndex:0]]; 
       } 
       if (indexPath.row == 3){ 
       cell.textLabel.text = [NSString stringWithFormat:@"%@:30 - %@:45", [listItems objectAtIndex:0], [listItems objectAtIndex:0]]; 
       } 
       if (indexPath.row == 4){ 
        cell.textLabel.text = [NSString stringWithFormat:@"%@:45 - %d:00", [listItems objectAtIndex:0], ([[listItems objectAtIndex:0] intValue] + 1)]; 
       } 

      } 

     } 


    if (dimension.selectedSegmentIndex == 1){ 
     Discount * discount = [tableData objectAtIndex:indexPath.row]; 
     cell.textLabel.text = discount.vendor; 
    } 

    return cell; 
} 




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [manager loadObjectsAtResourcePath:@"/route.json" objectClass:[Location class] delegate: self]; 

    RouteMapViewController * routemap = [[RouteMapViewController alloc] initWithNibName:@"RouteMapViewController" bundle:nil]; 
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" 
                     style: UIBarButtonItemStyleBordered 
             // style: UIBarButtonItemStylePlain 
             // style: UIBarButtonItemStyleDone 
                    target:nil 
                    action:nil]; 
    self.navigationItem.backBarButtonItem = backButton; 
    [backButton release]; 

    if (dimension.selectedSegmentIndex == 0){ 
      if (!indexPath.row) 
      { 
       // only first row toggles expand/collapse 
       [preference deselectRowAtIndexPath:indexPath animated:YES]; 

       NSInteger section = indexPath.section; 
       BOOL currentlyExpanded = [expandedSections containsIndex:section]; 
       NSInteger rows; 

       NSMutableArray *tmpArray = [NSMutableArray array]; 

       if (currentlyExpanded) 
       { 
        rows = [self tableView:preference numberOfRowsInSection:section]; 
        [expandedSections removeIndex:section]; 

       } 
       else 
       { 
        [expandedSections addIndex:section]; 
        rows = [self tableView:preference numberOfRowsInSection:section]; 
       } 


       for (int i=1; i<rows; i++) 
       { 
        NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i 
                    inSection:section]; 
        [tmpArray addObject:tmpIndexPath]; 
       } 

       UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

       if (currentlyExpanded) 
       { 
        [preference deleteRowsAtIndexPaths:tmpArray 
            withRowAnimation:UITableViewRowAnimationTop]; 

        cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown]; 

       } 
       else 
       { 
        [preference insertRowsAtIndexPaths:tmpArray 
            withRowAnimation:UITableViewRowAnimationTop]; 
        cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp]; 

       } 
      } 

    } 

這裏就是我希望它看上去像(該部分的字體是錯誤的,因爲我已經將它設置爲Arial粗體,但它不是在這裏)

enter image description here

這就是我得到擴大底部區域時:

enter image description here

這個爛攤子開始之後我滾動UITableV發生瀏覽以擴展後面的條目...我想它與更新視圖有關。

+0

我們無法看到圖像?你使用viewForSection方法嗎? – Ravin 2011-04-14 03:14:26

+0

不..沒有viewForSection方法。你指的是什麼圖像? – aherlambang 2011-04-14 03:23:48

+0

由於我無法看到圖像,請告訴您發生了什麼奇怪的事情? – Ravin 2011-04-14 03:27:03

回答

0

這是細胞再利用問題。每次重新使用表格單元格時,都需要重置/設置每個單元格屬性。

例如cell.accessoryView是上次使用單元格之前發生的任何事情。嘗試將其設置爲零。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    // Creating NEW cell 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
} 

// Now you are using either NEW or OLD cell 
// You MUST set each cell property that can be different 

// code to customize cell for certain purpose 

return cell; 

}

根據部分和行,你似乎是設置cell.textLabel.font,cell.textLabel.text,cell.accessoryView,cell.contentView.backgroundColor,cel.textLabel.backgroundColor等等

小區複用意味着有時你會得到一箇舊的小區,因爲它最後一次使用了。因此,您需要設置之前設置的所有這些值。如果某些值始終相同,那麼在創建分支新單元格時只定義一次。

是的,這是很多工作。您可以更改所有這些long if子句切換案例,開始使用通過索引訪問的預定義​​數組,以及可能爲不同單元格的Interface Builder XIBs。如果你願意的話,你也可以爲不同類型的單元使用幾種不同的cellIdentifiers。在這種情況下,重複使用不是問題,但容易犯錯誤...

+0

嗯...我在哪裏重置? – aherlambang 2011-04-14 07:00:05

+0

更新了我的答案 – JOM 2011-04-14 07:31:19

0

要更改行外觀,您應該在cellForRowAtIndexPath方法中更改它們。在該方法中進行適當的更改。

改變標頭的tableView你可以用下面的代碼...

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 


     UIView *tempView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)]; 
     [tempView setBackgroundColor:[UIColor whiteColor]]; 
     UILabel *tempLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 44)]; 
     [tempLabel setBackgroundColor:[UIColor clearColor]]; 
     [tempLabel setTextColor:[UIColor blackColor]]; 
     [tempLabel setFont:[UIFont boldSystemFontOfSize:16.0]]; 
     [tempLabel setText:[sectionTitles objectAtIndex:section]]; 
     [tempView addSubview:tempLabel]; 
     [tempLabel release]; 
     return [tempView autorelease]; 

} 

感謝,

+0

這不起作用...我希望你看看代碼,看看爲什麼這不起作用..這是一個擴大/摺疊行 – aherlambang 2011-04-14 05:09:45