2013-08-16 2 views
0

아무도 도와 줄 수 있습니까? 지난 1 주 동안 셀을 확장하는 중입니다. 마지막으로 하위 메뉴를 추가 할 수 있습니다. 나는 두 개의 사용자 정의 셀을 설계하고 plist를 사용하여 메뉴와 하위 메뉴를 추가했다. 그것은 잘 작동 메뉴와 하위 메뉴를 추가했습니다. 이제 내 문제는 이미지와 버튼을 1,2,4,6 행에만 추가하고 싶습니다. indexpath.row를 사용합니다. 그러나 할당 된이 코드는 모든 행에 이미지와 버튼을 할당하지만, 행 1,2,4에 추가하고 싶습니다. , 6 오직 호 내가 할 수있는이 하나의 도움이 pls 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

같은 오/P는 몇 가지 중 하나가 나를 도와 싶어?

답변

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

감사를 작동하지 않습니다 –

0

dequeueReusableCellWithIdentifier: 메서드를 사용하고 있기 때문에 인덱스 경로 2, 4, 6 등에서 tableView의 다른 셀에 셀을 제공합니다. 대신 배열을 사용하여 셀을 저장 한 다음 배열에서 검색하십시오. 그리고 나서 indexpath.row를 사용할 수 있습니다.

+0

내가 배열에 저장 전지 및 사용 indexpath.row하지만 gohel @nitin –

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]; 
    } 
관련 문제