iOS 数组截取自定义个数

_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"];

}

posted on 2017-11-09 11:26  廖利君  阅读(4723)  评论(0编辑  收藏  举报