var teacherArray=[];
我创建了一个数组变量。 我正在创建一个包含键和值的数组。并将这些数据推送给老师。
random={
teacherId:TeacherId,
day:day,
periodCount:period,
class:Studentclass,
section:Studentsection,
startTime:schoolStartTime,
endTime:schoolEndTime
};
teacherArray.push(random);
console.log(teacherArray);
在控制台中,我可以看到创建的数组。但是当点击提交按钮时,我正在调用数组
teacherLength=teacherArray.length;
for (let k=0;k<=teacherLength;k++)
{
var teachId=teacherArray[k].teacherId;
console.log(teachId);
}
在控制台中显示teacherId,但下一行显示错误
答案 0 :(得分:2)
数组是基于零索引的。因此,当您编写k<=teacherLength
时,您要求的数量超过数组的数量。那应该改为
k<teacherLength
答案 1 :(得分:1)
数组为0索引。你有,
- (void)awakeFromNib
{
[super awakeFromNib];
self.itemSize = CGSizeMake((self.collectionView.frame.size.width/2)+100,self.collectionView.frame.size.height );
//self.minimumInteritemSpacing = 10.0;
self.minimumLineSpacing = 10.0;
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.sectionInset = UIEdgeInsetsMake(0, 30.0, 0, 30.0);
}
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity {
NSInteger itemsCount = [self.collectionView.dataSource collectionView:self.collectionView numberOfItemsInSection:0];
// Imitating paging behaviour
// previous offset and scroll direction
if ((self.previousOffset > self.collectionView.contentOffset.x) && (velocity.x < 0.0f)) {
self.currentPage = MAX(self.currentPage - 1, 0);
} else if ((self.previousOffset < self.collectionView.contentOffset.x) && (velocity.x > 0.0f)) {
self.currentPage = MIN(self.currentPage + 1, itemsCount - 1);
}
// Update offset by using item size + spacing
CGFloat updatedOffset = (self.itemSize.width + 5) * self.currentPage;
self.previousOffset = updatedOffset;
return CGPointMake(updatedOffset, proposedContentOffset.y);
}
制作它,
for (let k=0;k<=teacherLength;k++)
答案 2 :(得分:1)
您的代码正在阅读 额外项目 ,您需要更换
self.yourView.setNeedsUpdateConstraints()
与
for (let k=0;k<=teacherLength;k++)