logo
down
shadow

UISwitch in a table selects multiple cells when switch state on


UISwitch in a table selects multiple cells when switch state on

By : lkw92
Date : November 22 2020, 03:01 PM
should help you out This is due to cells being re-used. When you scroll the tableView, some rows go off screen; the associated cells are placed into a queue. Other rows appear on screen; the cellForRowAt method is called and takes a cell from the queue (hence dequeueReusableCell), to be used for the new row. If the switch was on when the cell went off screen, it will still be on when it appears on screen - even though it now relates to a completely different row.
The solution is to maintain an array which indicates whether the switch for a given row should be on or off. Then you can use that array to correctly set the switch on or off in the cellForRowAt method.
code :


Share : facebook icon twitter icon
If I add a UISwitch control to each of my table view cells, how can I tell which cell it belongs to?

If I add a UISwitch control to each of my table view cells, how can I tell which cell it belongs to?


By : user2210279
Date : March 29 2020, 07:55 AM
it helps some times In your action method, you can cast the superview of the passed-in UISwitch (which is the UITableViewCell itself), then pass that to the tableView's indexPathForCell method.
indexPathForCell will return a NSIndexPath object, which you can then use to index to your datamodel to modify. Then all you gotta do is call reloadData on your tableView.
Objective-C UISwitch change state of another UISwitch

Objective-C UISwitch change state of another UISwitch


By : Hiroka Koizumi
Date : March 29 2020, 07:55 AM
With these it helps Add event handler for your switch1 value changed, you can do it in storyboard as well):
code :
[switch1 addTarget:self action:@selector(changeSwitch:) forControlEvents:UIControlEventValueChanged];
-(void)changeSwitch:(id)sender{
    UISwitch *s = (UISwitch*)sender;
    //Change value on second switch
    [s2 setOn:!s.isOn];
}
How to retrieve UISwitch state in each of my UITableView cells?

How to retrieve UISwitch state in each of my UITableView cells?


By : Sawy
Date : March 29 2020, 07:55 AM
wish of those help You save that value just like any other value you use in a table view -- in an array that's your data source. Given what you show in your image, your data source should be an array of dictionaries with keys for the menu item, price, and switch state. In cellForRowAtIndexPath, you would set the state of the switch based on the value ( a BOOL) in your array.
UISwitch in table view were not able to disabled by a custom switch

UISwitch in table view were not able to disabled by a custom switch


By : prakash patil
Date : March 29 2020, 07:55 AM
This might help you Try bringing your switchObj declaration code inside UITableView's cellForRowAtIndex method and delete from top.
code :
UISwitch *switchObj;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.textLabel.text = [infoDetail objectAtIndex:indexPath.row];  
    UISwitch *switchObj = [[UISwitch alloc] initWithFrame:CGRectMake(1.0, 1.0, 20.0, 20.0)];
    switchObj.transform = CGAffineTransformMakeScale(1, 1);
    [switchObj addTarget:self action:@selector(alarmOn:) forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)];
    cell.accessoryView = switchObj;

    if (customSwitch.isOn) {
        switchObj.enabled = YES;
    }
    else{
        switchObj.enabled = NO;
    }
    return cell;
}
-(void)stateChanged:(UISwitch *)switchState{
    [myTableView reloadData];
}
Saving state of UISwitch of cells in UITableView to pass data to source controller

Saving state of UISwitch of cells in UITableView to pass data to source controller


By : wenjluo26
Date : March 29 2020, 07:55 AM
seems to work fine If you are using a stroyboard you can create an unwind segue (to go back from FilterTableViewCtrl to ListTableViewCtrl). This case you can save data in the unwind action method which is located in ListTableViewCtrl.
code :
- (IBAction)unwindToList:(UIStoryboardSegue*)unwindSegue
{
    UIViewController* sourceViewController = unwindSegue.sourceViewController;
    if ([sourceViewController isKindOfClass:[FilterTableViewCtrl class]])
    {
        FilterTableViewCtrl* filter = (FilterTableViewCtrl*)unwindSegue.sourceViewController;
        //read state of filter, and update list (self)
    }
}
Related Posts Related Posts :
  • Azure App Service Push - registration endpoint
  • Select multiple item in a tableView
  • Apartment number / Unit number returned from google places api
  • How to align UICollectionViewCells from left to right?
  • How can I add search option on UIPickerview in ios?
  • Scenekit - physicsWorld setup to prevent kinematic nodes to intersect
  • Carthage Build Failed
  • swift 3, ios 10 - push notification firebase is not received
  • App Xcode 8 to Xcode 9 - Prevent Auto Hide Status Bar
  • How to pop back to a TableViewController?
  • Is UserDefaults thread safe in Swift? How to check it?
  • iOS App with no story board (Size classes and Device type limitations)
  • AudioKit issue, mic not sensitive enough
  • send indexpath to Firebase (like button)
  • UIColor saturation brightness values different from the regular saturation luminosity values of a color
  • JSON: Extracting Data Using Alamofire and Swift 3
  • invalid_grant on OAuth2 request when obtaining access_token from SSO in App
  • ABNewPersonViewController using contact framework
  • Design iOS button scalable
  • Subclassing NSLayoutConstraint constant based on screen height
  • Migrate Realm 2.x List of Object to Realm 3.x List of String (or other primary type)
  • Using BLE RSSI value for connect-by-proximity
  • Backgroundcolor of NSTextfield
  • Firebase modifying uid in every view
  • How to rotate orientation?
  • Upload a new version of ios app to app store?
  • UIScrollView do not scroll down to its original position when keyboard disappear
  • What is the most effective way of tracking time/dates in an iOS/Swift application?
  • WatchKit get user's preferred handedness
  • Location permission dialog is shown and immediately disappears
  • Combine these regex expressions
  • How to save an array of HKQuantitySamples (heart rate) to a workout
  • RestKit mapping with parent key as attribute and key contains parenthesis
  • How to add UITextField programmatically to UIScrollView without breaking constraints?
  • How to make a view height grow depending on its child views contents
  • Some users experiencing NSInternalInconsistencyException crash
  • Mark successful siesta response as error
  • Using a modified PDFTron Tools Framework
  • How can I use universal links when I call openURL inside my own app?
  • NSArray of doubles iOS (Objective c)
  • Passing data from embedded PageViewController to parent View Controller
  • Reuse item inside a row when scrolling
  • Realm Relation how to implement
  • error: generic parameter 'T' could not be inferred in swift
  • Adding shadow to UITextView makes text expand out of UITextViewFrame
  • How to change the string in seconds to minutes in Swift3?
  • Search bar is refreshing the index of my table view cell
  • Decodable multiple DateDecodingStrategies
  • The spoiled simulator xcode 9. As a broken TV
  • How to set width of TouchableWithoutFeedback depends on Text in react-native?
  • How to show an UIActivityIndicatorView whitin UITableViewController?
  • UItextfield text in Custom UITableViewCell prints nil when i scroll up in SE
  • How to append or add multiple items to an array in Swift 4
  • SQLite WHERE column MATCH parameter binding
  • Load iframe tag (video) from html string into web view using Swift 3
  • How to determine when a custom UICollectionViewCell is 100% on the screen
  • Get safe area inset top and bottom heights
  • Cordova iOS project: xCode 9 missing some entitlements
  • UITableView not scrolling properly to bottom until a cell is clicked
  • Reloading Particular Row of PickerView in swift
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org