2013-08-16 60 views
0

任何人都可以幫助我嗎?我在過去的一個星期裏爲擴展單元格工作最後,我可以在其中添加子菜單。我設計了兩個自定義單元格,使用plist,我添加了菜單和子菜單。它運行良好,我添加了菜單和子菜單。現在我的問題是我想添加圖像和按鈕行1,2,4,6只使用indexpath.row我分配,但此代碼分配圖像和按鈕到所有行但我只想添加到行1,2,4 ,6只有我能做到這一點,請幫助我一些?如何在ios中單獨設計自定義單元格?

interface MyHomeView() 
{ 

    NSMutableArray *LeftPaneList; 
    NSArray *datalist; 

} 
@property (assign)BOOL isOpen; 
@property (nonatomic,retain)NSIndexPath *selectIndex; 
@end 

    - (void)viewDidLoad 
{ 
    [super viewDidLoad]; 


    NSString *path = [[NSBundle mainBundle] pathForResource:@"LeftPaneMenuList" ofType:@"plist"]; 
    LeftPaneList = [[NSMutableArray alloc] initWithContentsOfFile:path]; 



    self.isOpen=NO; 

} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [LeftPaneList count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (self.isOpen) { 
     if (self.selectIndex.section == section) { 
      return [[[LeftPaneList objectAtIndex:section] objectForKey:@"SubMenu"] count]+1;; 
     } 
    } 
    return 1; 
} 


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




    if (self.isOpen&&self.selectIndex.section == indexPath.section&&indexPath.row!=0) { 

     static NSString *CellIdentifier = @"MyHomeViewCell2"; 
     MyHomeViewCell2 *cell = (MyHomeViewCell2*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (!cell) { 
      cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0]; 
     } 
     NSArray *list = [[LeftPaneList objectAtIndex:self.selectIndex.section] objectForKey:@"SubMenu"]; 
     cell.name.text = [list objectAtIndex:indexPath.row-1]; 



     return cell; 


    } 

    else 
    { 
     static NSString *CellIdentifier = @"MyHomeViewCell"; 
     MyHomeViewCell *cell = (MyHomeViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (!cell) { 
      cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0]; 
     } 
     cell.tag=indexPath.row; 


     NSString *name = [[LeftPaneList objectAtIndex:indexPath.section] objectForKey:@"MenuName"]; 

cell.MyHomeMenuLabel.text = name; 
     return cell; 




} 
} 

#pragma mark - Table view delegate 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row == 0) { 
     if ([indexPath isEqual:self.selectIndex]) { 
      self.isOpen = NO; 
      [self didSelectCellRowFirstDo:NO nextDo:NO]; 
      self.selectIndex = nil; 

     }else 
     { 
      if (!self.selectIndex) { 
       self.selectIndex = indexPath; 
       [self didSelectCellRowFirstDo:YES nextDo:NO]; 

      }else 
      { 

       [self didSelectCellRowFirstDo:NO nextDo:YES]; 
      } 
     } 

    } 


    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 


- (void)didSelectCellRowFirstDo:(BOOL)firstDoInsert nextDo:(BOOL)nextDoInsert 
{ 
    self.isOpen = firstDoInsert; 


    [self.MyHomeTableView beginUpdates]; 

    int section = self.selectIndex.section; 
    int contentCount = [[[LeftPaneList objectAtIndex:section] objectForKey:@"SubMenu"] count]; 
    NSMutableArray* rowToInsert = [[NSMutableArray alloc] init]; 
    for (NSUInteger i = 1; i < contentCount + 1; i++) { 
     NSIndexPath* indexPathToInsert = [NSIndexPath indexPathForRow:i inSection:section]; 




     [rowToInsert addObject:indexPathToInsert]; 
    } 

    if (firstDoInsert) 
    { [self.MyHomeTableView insertRowsAtIndexPaths:rowToInsert withRowAnimation:UITableViewRowAnimationTop]; 
    } 
    else 
    { 
     [self.MyHomeTableView deleteRowsAtIndexPaths:rowToInsert withRowAnimation:UITableViewRowAnimationTop]; 
    } 



    [self.MyHomeTableView endUpdates]; 
    if (nextDoInsert) { 
     self.isOpen = YES; 
     self.selectIndex = [self.MyHomeTableView indexPathForSelectedRow]; 
     [self didSelectCellRowFirstDo:YES nextDo:NO]; 
    } 
    if (self.isOpen) [self.MyHomeTableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionTop animated:YES]; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 52; 
} 

這是原始的o/p! enter image description here

但我想O/P這樣enter image description here

有人幫我嗎?

回答

2

你需要指定IndexPath.section首先,你可以像IndexPath.row貝婁例檢查: -

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

    if(indexPath.section==0) 
    { 
     if(indexPath.row==0) 
     { 

      cell = [tableView dequeueReusableCellWithIdentifier:@"CellWithButton" forIndexPath:indexPath]; 
     } 
     else if(indexPath.row==2) 
     { 
      cell = [tableView dequeueReusableCellWithIdentifier:@"CellWithButton" forIndexPath:indexPath]; 

     } 
     else if(indexPath.row==2) 
     { 
      cell = [tableView dequeueReusableCellWithIdentifier:@"CellWithButton" forIndexPath:indexPath]; 

     } 
     else if(indexPath.row==4) 
     { 
      cell = [tableView dequeueReusableCellWithIdentifier:@"CellWithButton" forIndexPath:indexPath]; 

     } 
     else if(indexPath.row==6) 
     { 

      cell = [tableView dequeueReusableCellWithIdentifier:@"CellWithButton" forIndexPath:indexPath]; 
     } 
     else 
     { 
     cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
     } 
    } 

} 
+0

感謝@nitin gohel –

0

正如你正在使用dequeueReusableCellWithIdentifier:方法,它只是將索引路徑2,4,6等單元格給tableView中的其他單元格。而是使用數組來存儲您的單元格,然後從數組中檢索它。然後你可以使用indexpath.row

+0

我在陣列存儲單元,並使用indexpath.row,但它不工作 –

1

這樣做的簡單方法是在故事板中設置兩個不同的動態原型單元格,每個單元格都有自己的標識符。在cellForRowAtIndexPath中:根據indexPath出列正確類型的單元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell; 
    if (indexPath.row = 1 || indexPath.row = 2 || indexPath.row = 4 || indexPath.row = 6){ 
     cell = [tableView dequeueReusableCellWithIdentifier:@"CellWithButton" forIndexPath:indexPath]; 
    }else{ 
     cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    } 
相關問題