TableViewCell 中的 UISwitch - 切换时更改单元格的文本

发布于 2024-10-03 06:40:33 字数 1769 浏览 11 评论 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();
}

}

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

微凉 2024-10-10 06:40:33

以下是从操作方法中获取单元格的方法,不需要子类化 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";
}

Here is an approach to getting the cell from within the action method that does not require subclassing 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";
}
与之呼应 2024-10-10 06:40:33

您可以使用 -[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).

一人独醉 2024-10-10 06:40:33

因此,我对 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";

}

So I subclassed UISwtich and included property for storing the cell's index path, which can then be used in the action method:

- (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";

}

旧人 2024-10-10 06:40:33

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

@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!)
}

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!

@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!)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文