【问题标题】:How to repeat the data from array Start data up to End data and again Start data in IOS objective c如何在IOS目标c中重复从数组开始数据到结束数据的数据并再次开始数据
【发布时间】:2015-05-31 07:55:05
【问题描述】:

我想在 IOS 目标 c 中从数组 Start data 加载到 End data 和再次 Start data 时重复数据。我已经实现了以下代码。我已经使用@try { } @catch 处理异常并停止从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;
}

【问题讨论】:

    标签: ios objective-c arrays uitableview indexoutofboundsexception


    【解决方案1】:

    如果我正确理解了您的意图,那么对于您想要的,有一个特定的操作,它被称为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 中找到提醒。

    【讨论】:

    • 您可以通过将硬编码的 23 替换为数据源数组的计数来进一步调整它。
    • 我想在从数组加载时重复数据开始数据到结束数据,然后再次开始数据到结束数据重复开始数据到结束数据
    • 我知道我在做的不是好方法,所以我正在寻找答案。你怎么理解我的问题
    • @nischalhada 这段代码正是这样做的。你可以试试这个 sn-p 并且不要忘记设置大量的行数。
    • @nischalhada 我按照我的理解回答了你的问题。试试这个代码,如果它不能解决你的问题,那么请澄清你的问题。
    猜你喜欢
    • 1970-01-01
    • 2019-07-02
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2018-12-06
    相关资源
    最近更新 更多