您的位置:首页 > 移动开发 > IOS开发

iOS 数组截取自定义个数,tableView部分显示数组内容

2018-01-18 10:52 375 查看

_dataArr 这里面有10个元素,需求是只要5个

for(int i = 0;i<5;i++){//先拿5个,循环拿取        NSIndexSet *indexset = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,4)];//范围0,1,2个元素        if(_dataArr.count >5){//先判断数组里面的元素是否大于5个,再做截取操作  不然会崩            [_dataArr objectsAtIndexes:indexset];//再将新获取的元素添加至数组        }    } 如果是tabbaleview显示- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return _dataArr.count >5?5:_dataArr.count;//如果self.dataArr.count大于5就显示5个,不足5个就显示数组里面本有的个数,其他不需要改了}- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{   NSDictionary *dic = _dataArr[indexPath.row];//正常显示     cell.carNumLabel.text = dic[@"carCode"];}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: