Ändern Sie den Status von UISwitch mit animation programmgesteuert
Ich habe eine UITableView, die mit einigen der Zelle, die containt UISwitches:
UISwitch* actSwitch = (UISwitch*)[cell viewWithTag: SWITCH_TAG];
[actSwitch addTarget: self
action: @selector(actSwitchChanged:)
forControlEvents: UIControlEventValueChanged];
BOOL value = [appSettings.lock_when_inactive boolValue];
[actSwitch setOn: value animated:NO];
Und ich will auch überschreiben didSelectRowAtIndexPath:
Methode zum ausführen Umschalten des jeweiligen UISwitch:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellType = [self typeOfCellForIndexPath:indexPath];
if ( cellType == @"CellWithSwitch" )
{
UISwitch* actSwitch = (UISwitch*)[[[self tableView] cellForRowAtIndexPath:indexPath ] viewWithTag: SWITCH_TAG];
BOOL value = [actSwitch isOn];
[actSwitch setOn:!value animated:YES]; //<-- HERE IS THE PROBLEM
[self actSwitchChanged: actSwitch];
}
else if ( cellType == @"CellWithoutSwitch" )
{
//other actions
}
}
In beiden Situationen, entweder ich klicke auf den UISwitch direkt oder durch anklicken einer Zelle, ändert es Zustand und call ' s actSwitchChanged:
richtig.
Aber im Falle, wenn ich auf die Zelle, meine UISwitch nicht animieren, das spiegeln von einem Zustand zu einem anderen, es ändert nur seinen Zustand in einem einzigen moment.
So [actSwitch setOn:!value animated:YES]
ist nicht genug zu erzählen, um perfom animation?
Hier ist, wie ich & call konfigurieren Zelle:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
NSString *EnabledCellIdentifier = [self typeOfCellForIndexPath:indexPath];
cell = [tableView dequeueReusableCellWithIdentifier:EnabledCellIdentifier];
if (cell == nil)
{
cell = [self cellForType:EnabledCellIdentifier];
}
[self cofigureCell:cell forIndexPath:indexPath];
return cell;
}
Dies ist, wie ich konfigurieren Zelle:
- (void)cofigureCell:(UITableViewCell*)cell forIndexPath:(NSIndexPath*)indexPath {
switch ( indexPath.section )
{
case 0:
//not this
case 1:
{
switch ( indexPath.row )
{
case 0:
{
cell.textLabel.text = @"Limit passwords attempts";
UISwitch* actSwitch = (UISwitch*)[cell viewWithTag: SWITCH_TAG];
[actSwitch addTarget: self
action: @selector(actSwitchChanged:)
forControlEvents: UIControlEventValueChanged];
BOOL value = [appSettings.limit_password_attempts boolValue];
[actSwitch setOn: value animated:NO];
break;
}
//other rows of this section here are being configured
}
break;
}
case 2:
{
switch ( indexPath.row )
{
case 0:
{
cell.textLabel.text = @"Lock when inactive";
UISwitch* actSwitch = (UISwitch*)[cell viewWithTag: SWITCH_TAG];
[actSwitch addTarget: self
action: @selector(actSwitchChanged:)
forControlEvents: UIControlEventValueChanged];
BOOL value = [appSettings.lock_when_inactive boolValue];
[actSwitch setOn: value animated:NO];
break;
}
//other rows of this section here are being configured
}
break;
}
default:
break;
}
}
Aber wenn ich Debuggen Schritt für Schritt, und gehen Sie durch diesen Schritt:
[actSwitch setOn:!value animated:YES]; //<-- HERE IS THE PROBLEM
Den actSwitch verändert seinen Zustand nur nach [self actSwitchChanged: actSwitch];
geänderten Daten-Modell und fordert [self.tableView reloadData];
Vielleicht der Grund ist, dass ich zwei Zellen mit UISwitches, und es accures einige Konflikte zwischen Ihnen?
Kann sein, dass es einen besseren Weg, um UISwitch aus der Zelle dann dieser code von mir?:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellType = [self typeOfCellForIndexPath:indexPath];
if ( cellType == @"CellWithSwitch" )
{
UISwitch* actSwitch = (UISwitch*)[[[self tableView] cellForRowAtIndexPath:indexPath ] viewWithTag: SWITCH_TAG];
BOOL value = [actSwitch isOn];
[actSwitch setOn:!value animated:YES]; //<-- HERE IS THE PROBLEM
[self actSwitchChanged: actSwitch];
}
else if ( cellType == @"CellWithoutSwitch" )
{
//other actions
}
}
- Haben Sie überprüft, ob
actSwitch
anzeigen bekam von Zelle Untersichten ist nichtnil
? Können Sie uns den code, wo du fügen Sie diesen Schalter, um die Zelle? - Danke für die Antwort, ich habe den code.
- [actSwitch setOn:!Wert animated:YES] für mich funktioniert und animiert wie andere. Funktioniert in jedem IBAction oder Leere Anweisung.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Nur anrufen, um Ihre actSwitchChanged Funktion mit einer gewissen Verzögerung:
SWIFT 4
Erstellen Sie eine UITableViewCell-Unterklasse, mit der delegate-Protokoll. Ich fügte hinzu, ein Titel, label und einen Schalter, in etwa so:
In Ihrem UITableViewController fügen Sie die delegate-Methode etwa so:
In Ihrem UITableViewController fügen Sie wie folgt vor, um Ihre didSelectRowAtIndexPath: