0

我希望 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();
}

}

4

4 回答 4

2

这是一种从不需要子类化 UISwitch 的操作方法中获取单元格的方法:

- (void)switchAction:(id)sender {

     // Cast the sender as a UISwitch
     UISwitch *aSwitch = (UISwitch *)sender;

     // Cast the superview of aSwitch to a UITableViewCell
     UITableViewCell *cell = (UITableViewCell *)aSwitch.superview;

     // You could get an indexPath as follows (though you don't need it in this case)
     NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];    

     // Set the text of the label in your cell
     cell.textLabel.text = aSwitch.on ? @"Active"; : @"Disabled";
}
于 2011-02-16T01:09:45.580 回答
1

您可以使用-[UITableView cellForRowAtIndexPath:]. 您只需要知道单元格的索引路径(另请注意,您是在向 tableview 询问单元格,而不是调用委托方法)。

于 2010-11-19T23:05:36.870 回答
0

因此,我将 UISwtich 子类化并包含用于存储单元格索引路径的属性,然后可以在操作方法中使用该属性:

- (void)actSwitchChanged:(id)sender {

TLSwitchInCell* swEnabled = (TLSwitchInCell*)sender;

currentItem.itemEnabled = [NSNumber numberWithBool:swEnabled.on];

UITableViewCell* theCell = [self.tableView cellForRowAtIndexPath:swEnabled.cellIndexPath];

theCell.textLabel.text = swEnabled.on ? @"Active" : @"Disabled";

}

于 2010-11-20T16:29:02.910 回答
0

主题的另一种变化!如果您有很多带有开关按钮的设置字段,您可以使用类似这样的方法来获取与触发的开关按钮关联的标签文本!您可以使用通常的方法设置文本!

@IBAction func switch(sender: UISwitch) {
  let labelIndex = sender.superview!.subviews.indexOf({$0 is UILabel})
  NSLog("The text from the label associated with this switch is %@",
  (sender.superview!.subviews[labelIndex!] as! UILabel).text!)
}
于 2015-10-26T22:12:33.443 回答