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


i want repeat data while loading array start data end data , again start data in ios objective c. have implemented following code. have use @try { } @catch handel exception , stop crash index 0 beyond bounds empty array error want implement better way

@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; } 

if understood intentions correctly, there specific operation want, , it's called modulo. can single line of code:

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {     //set cell     nsuinteger index = indexpath.row % gradidentcolour.count;     [cell setimage:[uiimage imagenamed:@"grass"] andcolor:[gradientcolour  objectatindex:index]];     return cell; } 

the % operator finds reminder division of indexpath.row 23.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -