TableViewCell 中的 UISwitch - 切换时更改单元格的文本
我希望 TableViewCell 中的 UISwitch 在切换时将文本从“活动”更改为“禁用”,反之亦然,但当开关更改时,表视图中的所有数据都会消失。我正在使用“重新加载数据”,因为我不知道如何更改特定单元格的文本。
仅供参考,“当前项目”是具有 BOOL 属性“itemEnabled”的核心数据实体。
该开关仅在“编辑模式”期间可见。
我的“详细视图控制器”内的表视图单元格中有一个 UISwitch:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
NSString *cellDetail = nil;
static NSString *EnabledCellIdentifier = @"Enabled";
cell = [tableView dequeueReusableCellWithIdentifier:EnabledCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:EnabledCellIdentifier] autorelease];
UISwitch* actSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[cell setEditingAccessoryView:actSwitch];
[actSwitch addTarget:self action:@selector(actSwitchChanged:) forControlEvents:UIControlEventValueChanged];
if ([[currentItem valueForKey:@"itemEnabled"] boolValue]) {
cellDetail = @"Active";
actSwitch.on = YES;
} else {
cellDetail = @"Disabled";
actSwitch.on = NO;
}
[actSwitch release];
cell.textLabel.text = cellDetail;
return cell;
}
我有一个接收操作的方法:
- (void)actSwitchChanged:(id)sender {
UISwitch* swEnabled = (UISwitch*)sender;
NSManagedObjectContext* itemContext = [currentItem managedObjectContext];
currentItem.itemEnabled = [NSNumber numberWithBool:swEnabled.on];
NSError *error = nil;
if (![itemContext save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
I want a UISwitch within a TableViewCell to change the text from 'Active' to 'Disabled' and vice versa when switched but when the switch changes, all the data in my table view disappears. I'm using 'reload data' since I do not know how to change a specific cell's text.
FYI, 'current item' is a core data entity with a BOOL property 'itemEnabled'.
The switch is only visible during 'editing mode'.
I have a UISwitch in a table view cell within my 'detail view controller':
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
NSString *cellDetail = nil;
static NSString *EnabledCellIdentifier = @"Enabled";
cell = [tableView dequeueReusableCellWithIdentifier:EnabledCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:EnabledCellIdentifier] autorelease];
UISwitch* actSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[cell setEditingAccessoryView:actSwitch];
[actSwitch addTarget:self action:@selector(actSwitchChanged:) forControlEvents:UIControlEventValueChanged];
if ([[currentItem valueForKey:@"itemEnabled"] boolValue]) {
cellDetail = @"Active";
actSwitch.on = YES;
} else {
cellDetail = @"Disabled";
actSwitch.on = NO;
}
[actSwitch release];
cell.textLabel.text = cellDetail;
return cell;
}
I have a method to receive the action:
- (void)actSwitchChanged:(id)sender {
UISwitch* swEnabled = (UISwitch*)sender;
NSManagedObjectContext* itemContext = [currentItem managedObjectContext];
currentItem.itemEnabled = [NSNumber numberWithBool:swEnabled.on];
NSError *error = nil;
if (![itemContext save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下是从操作方法中获取单元格的方法,不需要子类化 UISwitch:
Here is an approach to getting the cell from within the action method that does not require subclassing UISwitch:
您可以使用
-[UITableView cellForRowAtIndexPath:]
获取特定单元格。您只需要知道单元格的索引路径(另请注意,您是向表格视图询问单元格,而不是调用委托方法)。You can get a specific cell with
-[UITableView cellForRowAtIndexPath:]
. You just need to know the index path of the cell (also note that you are asking the tableview for the cell, not calling the delegate method).因此,我对 UISwtich 进行了子类化,并包含用于存储单元格索引路径的属性,然后可以在操作方法中使用该属性:
}
So I subclassed UISwtich and included property for storing the cell's index path, which can then be used in the action method:
}
主题的另一个变体!如果您有很多带有开关按钮的设置字段,您可以使用类似的方法来获取与触发的开关按钮关联的标签文本!您可以使用常规方法设置文本!
Another variation on a theme! If you have lots of settings fields with switch buttons you can use something like this to grab the label text associated with the switch button that was trigger! You can set the text using usual methods!