2013-08-16 1 views
0

Может ли кто-нибудь мне помочь? Я работаю над расширением ячейки в течение прошлой недели. Наконец, я могу добавить в нее подменю. Я разработал две пользовательские ячейки, и с помощью plist я добавил меню и подменю к этому. Он хорошо работает и добавил меню и подменю. Теперь моя проблема заключается в том, что я хочу добавить изображение и кнопку в строку 1,2,4,6, используя только indexpath.row i, но этот код назначает изображение и кнопку для всех строк. Но я хочу добавить только к строке 1,2,4 , 6 только хо я могу сделать это PLS, кто-то мне помогает ???Как создать индивидуальную строку ячеек по отдельности в 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

Но я хочу о/р, как этот 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

Этот простой способ сделать это - настроить 2 разных динамических ячейки прототипа в раскадровке, каждый со своим собственным идентификатором. В 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]; 
    }