2013-06-25 2 views
1

마다 의 행 수가 서로 다른 UITableView가 있습니다.섹션 당 UITableView의 잘못된 셀 값

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

    static NSString *CellIdentifier= @"channel"; 
    ChannelsCell *cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if(!cell) { 
     cell =[[ChannelsCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    } 

    NSInteger rowsCountSection = [[arrayofChannelsCount objectAtIndex:indexPath.section] intValue];// this variable has the number of rows of every section 

    NSUInteger pos = indexPath.section*count+ indexPath.row; 

    cell.channelName.text=[arrayofChannelName objectAtIndex:pos]; 

    return cell; 
} 

내가 그는 수학 문제입니다 확신 해요 : 나는 올바른 결과를 달성하려고하는 그 코드를했다.

도움 주셔서 감사합니다.

편집 됨 : 그 엄청난 수의 267575552가 온거야 어디에서

'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 267575552 beyond bounds [0 .. 101]' 

:

NSMutableArray *arrayofChannelsOffsets=[[NSMutableArray alloc]initWithCapacity:[arrayofChannelsCount count]]; 
    NSNumber* zero = [NSNumber numberWithInteger:0]; 
    [arrayofChannelsOffsets addObject:zero]; 

    for (int j=1; j<[arrayofChannelsCount count]; j++) { 
      NSUInteger sum=(int)[arrayofChannelsOffsets objectAtIndex:j-1]+(int)[arrayofChannelsCount objectAtIndex:j-1]; 
      NSNumber* num = [NSNumber numberWithInteger:sum]; 
      [arrayofChannelsOffsets addObject:num]; 

      NSLog(@"%lu count offset:",(unsigned long)[arrayofChannelsOffsets count]); 
      } 

     for (int i= 0 ; i < indexPath.section ; i++) { 
      pos =[[arrayofChannelsOffsets objectAtIndex:i] intValue] + indexPath.row; 
       } 
     cell.channelName.text=[arrayofChannelName objectAtIndex:pos]; 

나는 이상한 오류가 있습니다.

답변

1

, 당신은 오히려 indexPath.section*count를 사용하는 것보다이 일하기 전에 모든 섹션의 행 수를 총 할 필요가 동일한 계산을 반복 각 셀마다 계속해서 arrayofChannelsCount과 같은 수의 새로운 NSArray *arrayofChannelsOffsets을 추가 할 수 있지만 개별 개수를 포함하지 않고 각 항목에 현재 개수보다 작은 섹션의 개수 합계가 포함되어야합니다. 그런 배열을 사용하면 문제

NSUInteger pos = [[arrayofChannelsOffsets objectAtIndex:i] intValue] + indexPath.row; 
+0

행 '값. 문제는 90 % 해결되었습니다. arrayofChannelName의 첫 번째 요소 (1)로 시작하는 이유와 0 요소가 아닌 이유를 알지 못합니다 (나중에 사용자가 알아챈 것처럼 코드를 최적화하려고합니다). – androniennn

+1

@androniennn 실수로 생각한 것 같습니다. 루프의'<= '를 루프의'<'로 바꾸면 문제가 해결됩니다. – dasblinkenlight

+0

큰 존경의 남자! 당신은 언제나처럼 최고입니다. :) 그리고 이제는 내가 말한 것처럼 코드를 최적화 할 것입니다. 매우 대단히 감사합니다, 당신은 내 하루를 확인합니다 :). – androniennn

0

시도 :

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

TableViewSuite를 참조하십시오. 코드가 다소 중복

NSUInteger pos = indexPath.row; 
for (int = 0 ; i < indexPath.section ; i++) { 
    pos += [[arrayofChannelsCount objectAtIndex:i] intValue]; 
} 
cell.channelName.text=[arrayofChannelName objectAtIndex:pos]; 

하는 것으로 :

이 같은 경우
+0

를 작성하여 루프를 피할 수있을 것 섹션의 행 수는 아니지만, 답변을 주셔서 감사합니다 – androniennn

관련 문제