Question

I have a UITableView and a UIScrollView. The scroll view is paged. In my table view, there is an image and a title (Custom Cell).

I want to change the image of the cell at any index according to the scroll views page, for example if the scroll view page is 4, the 4th cell's image will change (active coloured image), when I make the scroll view page 5, the 4th image will become normal (passive coloured image) and the 5th cell's image will change (active coloured image).

I want to change the images during scrollViewDidScroll. I could not find any possible solutions on internet. Is there a way to do this?

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // how do i get page number : 0-768-1536 ---->0 means page1, 768 page 2 ...
    float currentPos = myScroller.contentOffset.x;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section     {

return konuBaslikListesi.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath  *)indexPath{
return 90;
}


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

KonularCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KonularCell"];
if (cell == nil) {

    cell =[[[NSBundle mainBundle] loadNibNamed:@"KonularCell" owner:self options:nil] objectAtIndex:0];
    [cell setAccessoryType:UITableViewCellAccessoryNone];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}

cell.Konuismi.numberOfLines = 0;
[cell.Konuismi setLineBreakMode:NSLineBreakByWordWrapping];

cell.Konuismi.text=[[[konuBaslikListesi objectAtIndex:indexPath.row]     objectForKey:@"konu"] objectForKey:@"text"];

cell.imgKonuisaret.image=[UIImage imageNamed:@"menupassive.png"];

return cell;
}
Was it helpful?

Solution

Calculate the page number from the size of each page and the content offset of the scroll view. The page number should be stored in a property.

CGFloat oldPage = self.page;

CGFloat offset = scrollView.contentOffset.x;
CGFloat pageWidth = scrollView.frame.size.width;

CGFloat page = floorf(offset / pageWidth);

if (page != oldPage) {
    self.page = page;
    [self.tableView reloadData];
}

Then in your table view method (cellForRowAtIndexPath:) you would have something like:

if (self.page == indexPath.row) {
    cell.imgKonuisaret.image=[UIImage imageNamed:@"menuactive.png"];
} else {
    cell.imgKonuisaret.image=[UIImage imageNamed:@"menupassive.png"];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top