Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9115391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:25:15+00:00 2026-06-17T04:25:15+00:00

I have a UICollectionView where I represent data taken from a SQLite database. When

  • 0

I have a UICollectionView where I represent data taken from a SQLite database. When I scroll up and down the CollectionView I have problems with a UISwitch:

-(UICollectionViewCell *)collectionView:(UICollectionView *)cv 
     cellForItemAtIndexPath:(NSIndexPath *)indexPath {

 city_setlist *cellValue = [self.arr objectAtIndex:indexPath.row];

 UISwitch *onoff=(UISwitch *) [cell viewWithTag:106];
 onoff.tag=[cellValue._id_setlist intValue];
    
 if([cellValue._was_there isEqualToString:@"1"])
     [onoff setOn:YES animated:YES];
 else
     [onoff setOn:NO animated:YES];
}

If I don’t scroll, data is ok, but if I start scrolling up and down the CollectionView, the UISwitch is randomly on where it should be off and vice versa. What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:25:16+00:00Added an answer on June 17, 2026 at 4:25 am

    Cells can be reused in a tiling view. This code looks like it assumes the cell is not reused:

    UISwitch *onoff=(UISwitch *) [cell viewWithTag:106];
    onoff.tag=[cellValue._id_setlist intValue];
    

    This changes the tag of the switch to be something other than 106, I assume. This means that if the cell is later reused, it will no longer be 106, and the top line will return nil. Try removing the second line I’ve quoted. (You could also confirm this is the problem by logging the onoff value you get in the first line while scrolling.)

    In general, using tags is kinda mucky; you might find it cleaner by defining a subclass for your cell and hooking up the UISwitch to a property (via, say, an outlet).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UICollectionView within a UIViewController. In the collectionView cellForItemAtIndexPath: method, it creates
I have a CollectionViewController and a CollectionViewCell. I am fetching data from a database,
I have a UICollectionView which shows photos. I have created the collectionview using UICollectionViewFlowLayout
I have a UICollectionView that horizontally scrolls to show one UICollectionViewCell at a time.
Setup: I have a UICollectionView or UITableView that’s backed by a simple array data
I have a UICollectionViewController : - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return [self.pageTastes count]; }
I have UICollectionView with UICollectionViewCell created in storyboard. I customized cell by adding to
I currently have implemented a UICollectionView with a UICollectionViewCell that does complete a segue
I have a UICollectionView with a custom cell subclassing UICollectionViewCell . And in the
I have a UICollectionView that is made up of a custom UICollectionViewCell subclass. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.