_dataArr 這里面有20個元素,需求是只要3個
for(int i = 0;i<3;i++){//先拿3個,循環拿取
NSIndexSet *indexset = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,2)];//范圍0,1,2個元素
if(_dataArr.count >3){//先判斷數組里面的元素是否大於3個,再做截取操作 不然會蹦躂
[_dataArr objectsAtIndexes:indexset];//再將新獲取的元素添加至數組
}
}
如果是tabbaleview顯示
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _dataArr.count >3?3:_dataArr.count;//如果self.dataArr.count大於3就顯示3個,不足3個就顯示數組里面本有的個數,其他不需要改了
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *dic = _dataArr[indexPath.row];//正常顯示
cell.carNumLabel.text = dic[@"carCode"];
}