Apple docs에서 cellForRowAtIndexPath가 순서대로 지정하지 않는다는 것을 알고 있습니다. 즉, 먼저 Section 0, Index 0; 그렇다면 Section 0, Index 2; 그리고 나서 Section 1, Index 0 .... 등등. 나는 그것이 주위에 뛰어 오른다라고 생각한다.iOS : UITableViewController - cellForRowAtIndexPath 셀 생성
아래에서 더 좋은 방법은 무엇입니까? 현재 나는 오류가 계속
NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 2]'
오류가 나타납니다
B 나는 cellForRowAtIndexPath 주위 점프 생각 마크 *sectioned = NULL
, 그래서 조건은 건너 뛰고은 빈 배열로 전환되거나 잘못된로 이동하여 인덱스 수를 초과하는 C/조건.
... declared and synthesized
NSManaged *object;
@property (strong, nonatomic) NSNumber *sevenDaysSectionFilled;
@property (strong, nonatomic) NSNumber *fourteenDaysSectionFilled;
@property (strong, nonatomic) NSNumber *thirtyDaysSectionFilled;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
object = NULL;
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (indexPath.section == 0)
{
if ((SevenDaysArraySectioned == [NSNumber numberWithBool:YES]) && (sevenDaysSectionFilled != [NSNumber numberWithBool:YES]))
{
object = [pastSevenDayArray objectAtIndex:indexPath.row];
sevenDaysSectionFilled = [NSNumber numberWithBool:YES];
}
else if ((fourteenDaysArraySectioned == [NSNumber numberWithBool:YES]) && (fourteenDaysSectionFilled != [NSNumber numberWithBool:YES]))
{
object = [pastFourteenDaysArray objectAtIndex:indexPath.row];
fourteenDaysSectionFilled = [NSNumber numberWithBool:YES];
}
else if ((thirtyDaysArraySectioned == [NSNumber numberWithBool:YES]) && (thirtyDaysSectionFilled != [NSNumber numberWithBool:YES]))
{
object = [pastThirtyDaysArray objectAtIndex:indexPath.row];
thirtyDaysSectionFilled = [NSNumber numberWithBool:YES];
}
else if (indexPath.section == 1)
{
if ((fourteenDaysArraySectioned == [NSNumber numberWithBool:YES]) && (fourteenDaysSectionFilled != [NSNumber numberWithBool:YES]))
{
object = [pastFourteenDaysArray objectAtIndex:indexPath.row];
fourteenDaysSectionFilled = [NSNumber numberWithBool:YES];
}
else if ((thirtyDaysArraySectioned == [NSNumber numberWithBool:YES]) && (thirtyDaysSectionFilled != [NSNumber numberWithBool:YES]))
{
object = [pastThirtyDaysArray objectAtIndex:indexPath.row];
thirtyDaysSectionFilled = [NSNumber numberWithBool:YES];
}
else if (indexPath.section == 2)
{
if ((thirtyDaysArraySectioned == [NSNumber numberWithBool:YES]) && (thirtyDaysSectionFilled != [NSNumber numberWithBool:YES]))
{
object = [pastThirtyDaysArray objectAtIndex:indexPath.row];
thirtyDaysSectionFilled = [NSNumber numberWithBool:YES];
}
}
NSString *entityName = [[object entity]name];
cell.textLabel.text = [NSString stringWithFormat:@"%@ %i", entityName, [indexPath row]];
object = NULL;
return cell;
}
는 이러한 목표를 달성하기 위해 더 나은 방법이 있으면 알려 주시기 바랍니다 : 여기에
는 코드입니다. 감사.* 편집
이 조금 복잡하지만 여기있다 :
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
numberOfSections = 0;
if ([pastSevenDayArray count] > 0) numberOfSections++;
if ([pastFourteenDaysArray count] > 0) numberOfSections++;
if ([pastThirtyDaysArray count] > 0) numberOfSections++;
if ([pastSixtyDaysArray count] > 0) numberOfSections++;
if ([pastNinteyDaysArray count] > 0) numberOfSections++;
if ([pastArray count] > 0) numberOfSections++;
[self numberOfCells];
return numberOfSections;
}
numberOfCells
-(void)numberOfCells
{
for (int section = 0; section<numberOfSections; section ++)
{
if (section == 0)
{
if ([pastSevenDayArray count] > 0)
{
SevenDaysArraySectioned = [NSNumber numberWithBool:YES];
sectionZeroCellCount = [pastSevenDayArray count];
sectionZeroHeader = sevenDaysSectionLabel;
}
else if ([pastFourteenDaysArray count] > 0)
{
fourteenDaysArraySectioned = [NSNumber numberWithBool:YES];
sectionZeroCellCount = [pastFourteenDaysArray count];
sectionZeroHeader = fourteenDaysSectionLabel;
}
else if ([pastThirtyDaysArray count] > 0)
{
thirtyDaysArraySectioned = [NSNumber numberWithBool:YES];
sectionZeroCellCount = [pastThirtyDaysArray count];
sectionZeroHeader = thirtyDaysSectionLabel;
}
else if ([pastSixtyDaysArray count] > 0)
{
sixtyDaysArraySectioned = [NSNumber numberWithBool:YES];
sectionZeroCellCount = [pastSixtyDaysArray count];
sectionZeroHeader = sixtyDaysSectionLabel;
}
else if ([pastNinteyDaysArray count] > 0)
{
ninetyDaysArraySectioned = [NSNumber numberWithBool:YES];
sectionZeroCellCount = [pastNinteyDaysArray count];
sectionZeroHeader = ninetyDaysSectionLabel;
}
else if ([pastArray count] > 0)
{
pastArraySectioned = [NSNumber numberWithBool:YES];
sectionZeroCellCount = [pastArray count];
sectionZeroHeader = pastSectionLabel;
}
}
else if (section == 1)
{
if (([pastFourteenDaysArray count] > 0) && (fourteenDaysArraySectioned != [NSNumber numberWithBool:YES]))
{
fourteenDaysArraySectioned = [NSNumber numberWithBool:YES];
sectionOneCellCount = [pastFourteenDaysArray count];
sectionOneHeader = fourteenDaysSectionLabel;
}
else if (([pastThirtyDaysArray count] > 0) && (thirtyDaysArraySectioned != [NSNumber numberWithBool:YES]))
{
thirtyDaysArraySectioned = [NSNumber numberWithBool:YES];
sectionOneCellCount = [pastThirtyDaysArray count];
sectionOneHeader = thirtyDaysSectionLabel;
}
else if (([pastSixtyDaysArray count] > 0) && (sixtyDaysArraySectioned != [NSNumber numberWithBool:YES]))
{
sixtyDaysArraySectioned = [NSNumber numberWithBool:YES];
sectionOneCellCount = [pastSixtyDaysArray count];
sectionOneHeader = sixtyDaysSectionLabel;
}
else if (([pastNinteyDaysArray count] > 0) && (ninetyDaysArraySectioned != [NSNumber numberWithBool:YES]))
{
ninetyDaysArraySectioned = [NSNumber numberWithBool:YES];
sectionOneCellCount = [pastNinteyDaysArray count];
sectionOneHeader = ninetyDaysSectionLabel;
}
else if (([pastArray count] > 0) && (pastArraySectioned != [NSNumber numberWithBool:YES]))
{
pastArraySectioned = [NSNumber numberWithBool:YES];
sectionOneCellCount = [pastArray count];
sectionOneHeader = pastSectionLabel;
}
}
else if (section ==2)
{
if (([pastThirtyDaysArray count] > 0) && (thirtyDaysArraySectioned != [NSNumber numberWithBool:YES]))
{
thirtyDaysArraySectioned = [NSNumber numberWithBool:YES];
sectionTwoCellCount = [pastThirtyDaysArray count];
sectionTwoHeader = thirtyDaysSectionLabel;
}
else if (([pastSixtyDaysArray count] > 0) && (sixtyDaysArraySectioned != [NSNumber numberWithBool:YES]))
{
sixtyDaysArraySectioned = [NSNumber numberWithBool:YES];
sectionTwoCellCount = [pastSixtyDaysArray count];
sectionTwoHeader = sixtyDaysSectionLabel;
}
else if (([pastNinteyDaysArray count] > 0) && (ninetyDaysArraySectioned != [NSNumber numberWithBool:YES]))
{
ninetyDaysArraySectioned = [NSNumber numberWithBool:YES];
sectionTwoCellCount = [pastNinteyDaysArray count];
sectionTwoHeader = ninetyDaysSectionLabel;
}
else if (([pastArray count] > 0) && (pastArraySectioned != [NSNumber numberWithBool:YES]))
{
pastArraySectioned = [NSNumber numberWithBool:YES];
sectionTwoCellCount = [pastArray count];
sectionTwoHeader = pastSectionLabel;
}
}
else if (section ==3)
{
NSLog(@"Entered Section %i", section);
if (([pastSixtyDaysArray count] > 0) && (sixtyDaysArraySectioned != [NSNumber numberWithBool:YES])) {
sixtyDaysArraySectioned = [NSNumber numberWithBool:YES];
sectionThreeCellCount = [pastSixtyDaysArray count];
sectionThreeHeader = sixtyDaysSectionLabel;
}
else if (([pastNinteyDaysArray count] > 0) && (ninetyDaysArraySectioned != [NSNumber numberWithBool:YES]))
{
ninetyDaysArraySectioned = [NSNumber numberWithBool:YES];
sectionThreeCellCount = [pastNinteyDaysArray count];
sectionThreeHeader = ninetyDaysSectionLabel;
}
else if (([pastArray count] > 0) && (pastArraySectioned != [NSNumber numberWithBool:YES]))
{
pastArraySectioned = [NSNumber numberWithBool:YES];
sectionThreeCellCount = [pastArray count];
sectionThreeHeader = pastSectionLabel;
}
}
else if (section ==4)
{
NSLog(@"Entered Section %i", section);
if (([pastNinteyDaysArray count] > 0) && (ninetyDaysArraySectioned != [NSNumber numberWithBool:YES]))
{
ninetyDaysArraySectioned = [NSNumber numberWithBool:YES];
sectionFourCellCount = [pastNinteyDaysArray count];
sectionFourHeader = ninetyDaysSectionLabel;
}
else if (([pastArray count] > 0) && (pastArraySectioned != [NSNumber numberWithBool:YES]))
{
pastArraySectioned = [NSNumber numberWithBool:YES];
sectionFourCellCount = [pastArray count];
sectionFourHeader = pastSectionLabel;
}
}
else if (section ==5)
{
NSLog(@"Entered Section %i", section);
if (([pastArray count] > 0) && (pastArraySectioned != [NSNumber numberWithBool:YES]))
{
pastArraySectioned = [NSNumber numberWithBool:YES];
sectionFiveCellCount = [pastArray count];
sectionFiveHeader = pastSectionLabel;
}
}
}
}
numberOfRowsInSection
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
{
return sectionZeroCellCount;
}
else if (section == 1)
{
return sectionOneCellCount;
}
else if (section == 2)
{
return sectionTwoCellCount;
}
else if (section == 3)
{
return sectionThreeCellCount;
}
else if (section == 4)
{
return sectionFourCellCount;
}
if (section == 5)
{
return sectionFiveCellCount;
}
}
,536,913 63,210
편집 2
헤더 :
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30.0;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 150)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width - 10, 25)];
label.backgroundColor = [UIColor clearColor];
[label setFont:[UIFont boldSystemFontOfSize:16]];
label.textColor = [UIColor whiteColor];
if (section == 0)
{
label.text = sectionZeroHeader;
}
else if (section == 1)
{
label.text = sectionOneHeader;
}
else if (section == 2)
{
label.text = sectionTwoHeader;
}
else if (section == 3)
{
label.text = sectionThreeHeader;
}
else if (section == 4)
{
label.text = sectionFourHeader;
}
else if (section == 5)
{
label.text = sectionFiveHeader;
}
[headerView addSubview:label];
return headerView;
}
나는 당신의 논리가 올바르지 않을 수 있습니다 각 섹션에 대한 행의 수를 반환 할 생각은 단지 세 가지 요소를 포함 –
나는 그것을 확인한 배열로 tableviews 섹션 중 하나에 네 개의 행을 채우기 위해 시도하는 것 같은데 너무 여러 번. numberOfRowsInSection : cellForRowAtIndexPath의 올바른 셀과 일치하는 경우 올바른 값입니다. – user1107173
왜 sectioned = NULL을하고 있습니까? –