I have created a custom UITableViewCell
and a class for the cell. I then load this cell in my viewController into a tableview. The custom cell has a label and a UiSwitch. I have managed to get the switch to work as I want it to and change values that are needed to change.
I now wanted a separate UISwitch outside of the UITableView
- that when it is turned on - all the switches in the UITableView are turned on.
For some reason i cannot get this to work.
This is the code for the custom cell:
- (void)selectAllSwitches:(BOOL)selected
{
if (selected){
[self.selectSwitch setOn:YES animated:YES];
}else{
[self.selectSwitch setOn:NO animated:YES];
}
}
-(void)setMaillist:(BBMailList *)aMaillist
{
maillist = aMaillist;
if (maillist){
self.nameLabel.text = maillist.name;
if (self.maillist.isSubscribed){
self.selectSwitch.on = YES;
}else{
self.selectSwitch.on = NO;
}
}
}
- (IBAction)switchControl:(UISwitch *)sender
{
self.maillist.isSubscribed = !self.maillist.isSubscribed;
NSLog (@"IsSubScribed: %ul", self.maillist.isSubscribed);
if (self.maillist.isSubscribed){
self.selectSwitch.on = YES;
}else{
self.selectSwitch.on = NO;
}
}
Then in cellForRowAtIndexPath:
static NSString *mailListTableViewCellIdentifier = @"BBMailListTableViewCell";
if (tableView == self.maillistTableview ){
BBMailListTableViewCell * maillistTableviewCell = [self.maillistTableview dequeueReusableCellWithIdentifier:mailListTableViewCellIdentifier];
if (!maillistTableviewCell){
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:mailListTableViewCellIdentifier owner:self options:nil];
maillistTableviewCell = nib[0];
}
if ([self.mailLists count ] > 0){
NSMutableArray *newArray = [NSMutableArray arrayWithArray:self.mailLists];
[newArray removeLastObject];
[newArray removeLastObject];
[self.maillistTableview setContentSize:CGSizeMake(320, 400)];
BBMailList *maillist = newArray[indexPath.row];
maillistTableviewCell.maillist = maillist;
}
return maillistTableviewCell;
}
This code works as I need it to.
I then created an IBAction methon in my viewController for the UISwitch that is outside of the tableView
- (IBAction)selectAllSwitch:(UISwitch *)sender
{
BBMailListTableViewCell *switchButton = [[BBMailListTableViewCell alloc]init];
[switchButton selectAllSwitches:YES];
}
This runs the correct method in my UITableViewCell class - but the UITableView doesn't reflect the changes. I added [self.myTableView reloadData];
- but this did not solve the issue. Can anyone shed some light not his for me?
****UPDATE****
Updated Code for the UISwitch outside of the UITableView
- (IBAction)selectAllSwitch:(UISwitch *)sender
{
BBMailList *tempMailList = [[BBMailList alloc]init];
tempMailList.isSubscribed = YES;
[self.maillistTableViewCell setMaillist:tempMailList];
[self.maillistTableViewCell selectAllSwitches:YES];
[self.maillistTableview reloadData];
}
Each mailistItem is a NSObject I use to store data with. The above code runs the correct methods - but it seems I am not keeping the state maybe and when [self.mayTableView reloadData]; runs - it resets the tableview data to original state?
The logic to solve the problem would be:
1) From your code it looks that the on/off functionality is dependent on your maillist.isSubscribed
property. In your IBAction
of viewcontroller, set it to true
.
2) Call [self.myTableView reloadData];
in this IBAction. So once the table loads cell the array's element will help the switch set to ON/OFF.
Hope it helps!
EDIT
It seems like you are losing your track. Because in your IBAction of your viewController you are creating a new array of type BBMailList
. Try like this:
Assuming your self.mailLists
is a collection of BBMailList
type.
Now:
- (IBAction)selectAllSwitch:(UISwitch *)sender
{
// loop through each array element set it to true.
for (BBMailList *tempMailList in self.mailLists)
{
tempMailList.isSubscribed = Yes;
}
// reload table data so once table
[self.maillistTableview reloadData];
}
and in cellForRowAtIndexPath
change your setting up of mail list like this:
if ([self.mailLists count ] > 0){
NSMutableArray *newArray = [NSMutableArray arrayWithArray:self.mailLists];
[newArray removeLastObject];
[newArray removeLastObject];
[self.maillistTableview setContentSize:CGSizeMake(320, 400)];
BBMailList *maillist = newArray[indexPath.row];
[maillistTableviewCell setMaillist: maillist]; // <--- Changed this line
}
Add UISwitch
to all cells and in cellForRowAtIndexPath
hide UISwitch
in cell you dont want to display it.
You need to add:
1)Array for state(on/off switch state)
2)Array for state(hide/unhide switch)on cells
Then change state's array as your application logic and when you will reload UITableView
data it should display data as per state's array values eg
arrayHideUnhide = (@"YES",@"NO",@"YES",@"NO");
arrayOnOff = (@"ON","OFF",@"ON");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.