如何重复数组开始数据到结束数据的数据,然后再次开始数据 IOS objective c

How to repeat the data from array Start data up to End data and again Start data in IOS objective c

我想在从数组 Start data 加载到 End data 时重复数据,然后在 IOS objective c 中再次加载数据 Start data。我已经实现了以下代码。我已经使用 @try { } @catch 来 Handel 异常并停止从 index 0 beyond bounds for empty array error 崩溃,但我想实现一些更好的方法

@interface CategoryVC ()
{
    NSArray* GradientColour;

}

- (void)viewDidLoad {
    [super viewDidLoad];
     /*Gradient Colours*/
    GradientColour = @[CategoryEntertainmen2,CategoryGroceries2,CategoryAutomobile, CategoryClothing
                        , CategoryComputer,Categoryeducation,CategoryElectronics,CategoryEntertainment,CategoryGroceries,CategoryHealthButy,CategoryHome,CategoryResturant,CategoryToys,CategoryEntertainmen2,
        CategoryGroceries2,CategoryHealthButy2,CategoryHome2,CategoryResturant2,CategoryToys2,CategoryFlowers,CategoryBreakfast,
        CategorySpicyFood,CategoryFuriture 
                       ];


}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

      /*Gradient Colours*/
    if (indexPath.row <23)
    {
      [cell setImage:[UIImage imageNamed:@"Grass"] andColor:[ GradientColour  objectAtIndex:indexPath.row]];

    }else{
        @try {
            [cell setImage:[UIImage imageNamed:@"Grass"] andColor:[ GradientColour  objectAtIndex:indexPath.row-23]];
        } @catch (id theException) {
            /*Handell Crash Conditions*/
            [cell setImage:[UIImage imageNamed:@"Grass"] andColor:[ GradientColour  objectAtIndex:5]];
        }

    }

    return cell;
}

如果我没理解错你的意思,有一个具体的操作可以满足你的要求,叫做modulo。你可以用一行代码来完成:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //Set up cell
    NSUInteger index = indexPath.row % GradidentColour.count;
    [cell setImage:[UIImage imageNamed:@"Grass"] andColor:[GradientColour  objectAtIndex:index]];
    return cell;
}

% 运算符从 indexPath.row 除以 23 找到提醒。