iphone - UITableViewCell repeating UIImages -
this again more of math problem else. have 10 pictures in resource bundle
sample_pic1.jpg sample_pic2.jpg sample_pic3.jpg ... sample_pic10.jpg i have table view 100 rows. want show 10 sample pictures every 10 rows in cell image. keep repeating every 10 rows. problem have cannot think of logic repeat images?
this have.
note: here indexpath.row keeps going 0 through 99. can use
// customize appearance of table view cells. - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { //nslog(@"inside cellforrowatindexpath ... indexpath.row: %d", indexpath.row); static nsstring *cellidentifier = @"cell"; // try retrieve table view now-unused cell given identifier. uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; // if no cell available, create new 1 using given identifier. if (cell == nil) { // use default cell style. cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier]; }//nil-check int imagecounter = (indexpath.row+1); //ofcourse logic doesn't work rows 21 , up! if (imagecounter > 10) { imagecounter = (indexpath.row+1) - 10; } nslog(@"indexpath.row: %d ... imagecounter: %d ...", indexpath.row, imagecounter); nsmutablestring *tmpimgstr = [nsmutablestring stringwithformat:@"sample_pic%d.jpg", imagecounter]; uiimage *myimage = [uiimage imagenamed:tmpimgstr]; cell.imageview.image = myimage; }
use modulo operator repeat image number each 10 rows, , add 1 take account images numbered starting 1:
int imagecounter = (indexpath.row % 10) + 1; nsmutablestring *tmpimgstr = [nsmutablestring stringwithformat:@"sample_pic%d.jpg", imagecounter];
Comments
Post a Comment