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

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM