I am trying to subclass UITableViewController by providing me with a UITableView with my application settings. I have a problem reloading UISwtitch status change data.
The table has 3 sections:
section 1 - one line constantly
section 2 - UISwitch in the first line making this section 1 or 3 lines (depending on the state of UISwitch)
section 3 - UISwitch in the first row making this section 1 or 3 rows (depending on the state of UISwitch), but with a different UISwitch and additional row data.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch ( section )
{
case 0:
return 1;
break;
case 1:
if ( limit_password_attempts )
return 3;
else
return 1;
break;
case 2:
if ( lock_when_inactive )
return 3;
else
return 1;
break;
default:
return 1;
break;
}
}
These two UISwitch-s in the front rows of the second and third sections change BOOL, respectively
BOOL limit_password_attempts;
BOOL lock_when_inactive;
, UISwitch ( exmpl) , , 2 . , , . ( UISwitch ), , , , .
, , , .
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
static NSString *EnabledCellIdentifier = @"Enabled";
cell = [tableView dequeueReusableCellWithIdentifier:EnabledCellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc]
initWithStyle: UITableViewCellStyleDefault
reuseIdentifier: EnabledCellIdentifier]
autorelease];
cell.detailTextLabel.enabled = YES;
switch ( indexPath.section )
{
case 0:
cell.textLabel.text = @"Banks settings";
break;
case 1:
{
switch ( indexPath.row )
{
case 0:
{
cell.textLabel.text = @"Limit passwords attempts";
UISwitch* actSwitch = [[UISwitch alloc] initWithFrame: CGRectZero ];
[cell setEditingAccessoryView: actSwitch];
[actSwitch setTag: indexPath.section];
[actSwitch addTarget: self
action: @selector(actSwitchChanged:)
forControlEvents: UIControlEventValueChanged];
[self.view addSubview:actSwitch];
[actSwitch setOn:YES animated:NO];
[actSwitch setOn:NO animated:NO];
actSwitch.on = NO;
[cell addSubview: actSwitch];
cell.accessoryView = actSwitch;
[actSwitch release];
break;
}
case 1:
cell.textLabel.text = @"Lock after 3 atempts";
break;
case 2:
cell.textLabel.text = @"Lock for 30 min";
break;
default:
break;
}
break;
}
case 2:
{
switch ( indexPath.row )
{
case 0:
{
cell.textLabel.text = @"Lock when inactive";
UISwitch* pactSwitch = [[UISwitch alloc] initWithFrame: CGRectZero ];
[cell setEditingAccessoryView: pactSwitch];
[pactSwitch setTag: indexPath.section];
[pactSwitch addTarget: self
action: @selector(actSwitchChanged:)
forControlEvents: UIControlEventValueChanged];
[self.view addSubview:pactSwitch];
[pactSwitch setOn:YES animated:NO];
[pactSwitch setOn:NO animated:NO];
pactSwitch.on = NO;
[cell addSubview: pactSwitch];
cell.accessoryView = pactSwitch;
[pactSwitch release];
break;
}
case 1:
cell.textLabel.text = @"Lock when inactive for 20 min";
break;
case 2:
cell.textLabel.text = @"Unlock key";
break;
default:
break;
}
break;
}
default:
NSLog(@"%d", indexPath.section );
break;
}
}
return cell;
}
, - , onec, - , , , . . node . , , .
, ?
, . MVC? ? ? , ?
.
.
. .
reloadData UISwtitch:
[pactSwitch setTag: indexPath.section]
[actSwitch addTarget: self
action: @selector(actSwitchChanged:)
forControlEvents: UIControlEventValueChanged]
actSwitchChanged:
-(void) actSwitchChanged: (UISwitch*) pSwitch {
if ( [pSwitch tag] == 1 )
limit_password_attempts = !limit_password_attempts ;
else if ( [pSwitch tag] == 2 )
lock_when_inactive = !lock_when_inactive;
[self.tableView reloadData];
}
reloadData viewTable, , .
?