gpt4 book ai didi

ios - 识别方法中的表格单元格

转载 作者:行者123 更新时间:2023-11-29 02:52:19 25 4
gpt4 key购买 nike

我正在尝试实现FlatUIKit在我的 ViewController 中 UITableView 的自定义单元格中,它根据开关状态将值返回给 NSUserDefaults 。作为通常的开关,我将方法“saveSwitchState”连接到 valueChanged socket 。问题是我想将状态传递给用户默认值并使用标签作为键,但该方法无法访问单元格和标签。我尝试添加:

static NSString *CellIdentifier = @"Cell";
setupOneCell *cell = (setupOneCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

到了方法,但还是没有运气。如果有更好的方法来保存带有 valueChanged 方法的开关状态,我会洗耳恭听。下面是我的 ViewController 和自定义单元。

ViewController.m:

#import "setup1ViewController.h"
#import "setup1TableView.h"
#import "setupOneCell.h"
#import "Social.h"
#import "FUISwitch.h"
#import "UIFont+FlatUI.h"
#import "UIColor+FlatUI.h"

@interface setup1ViewController ()

- (IBAction)saveSwitchState:(id)sender;

@end

@implementation setup1ViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

self.oneTableView.dataSource = self;
self.oneTableView.delegate = self;

self.medias = [[NSMutableArray alloc] init];

Social *media = [[Social alloc] init];
media.socialMedia = @"Facebook";
[self.medias addObject:media];

media = [[Social alloc] init];
media.socialMedia = @"Twitter";
[self.medias addObject:media];

media = [[Social alloc] init];
media.socialMedia = @"Instagram";
[self.medias addObject:media];

media = [[Social alloc] init];
media.socialMedia = @"Tumblr";
[self.medias addObject:media];

media = [[Social alloc] init];
media.socialMedia = @"Gmail";
[self.medias addObject:media];

media = [[Social alloc] init];
media.socialMedia = @"Linked In";
[self.medias addObject:media];

media = [[Social alloc] init];
media.socialMedia = @"GitHub";
[self.medias addObject:media];

media = [[Social alloc] init];
media.socialMedia = @"You Tube";
[self.medias addObject:media];

media = [[Social alloc] init];
media.socialMedia = @"Vine";
[self.medias addObject:media];

media = [[Social alloc] init];
media.socialMedia = @"SoundCloud";
[self.medias addObject:media];


//self.setup1Table.editing = YES;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.medias count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
setupOneCell *cell = (setupOneCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

cell.cellSwitch.onColor = [UIColor turquoiseColor];
cell.cellSwitch.offColor = [UIColor cloudsColor];
cell.cellSwitch.onBackgroundColor = [UIColor midnightBlueColor];
cell.cellSwitch.offBackgroundColor = [UIColor silverColor];
cell.cellSwitch.offLabel.font = [UIFont boldFlatFontOfSize:14];
cell.cellSwitch.onLabel.font = [UIFont boldFlatFontOfSize:14];

Social *media = (self.medias)[indexPath.row];

cell.socialName.text = media.socialMedia;


return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
return;
}

- (IBAction)saveSwitchState:(id)sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if ([cell.cellSwitch isOn])
[defaults setBool:YES forKey:cellLabel];
else
[defaults setBool:NO forKey:cellLabel];
}

@end

setupOneCell.h:

#import <UIKit/UIKit.h>
#import "FUISwitch.h"
#import "UIFont+FlatUI.h"
#import "UIColor+FlatUI.h"

@interface setupOneCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *socialName;
@property (weak, nonatomic) IBOutlet FUISwitch *cellSwitch;

@end

最佳答案

基本思想是让您的单元格监听开关值的变化,然后通过委托(delegate)调用向您的 View Controller 报告该变化。然后在您的 View Controller 中,您可以查找与该单元格关联的数据并使用它来修改 NSUserDefaults

如何做到这一点:

在单元格的设置代码中,您应该将单元格添加为开关的目标,而不是让 View Controller 成为目标。 (可能在您的单元格的 -awakeFromNib 中,但它也可以在某种 -configureWithSocialMedia: 方法中工作。)

- (void)awakeFromNib {
[self.cellSwitch addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
}

- (void)switchValueChanged:(id)sender {
[self.delegate setupOneCell:self witchValueDidChange:[self.cellSwitch isOn]];
}

然后使用类似 -setupOneCell:switchValueDidChange: 的方法创建协议(protocol),传递您的单元格和开关值。让您的单元接受一个遵循此协议(protocol)的代表。

协议(protocol)的代码应该是这样的:

@protocol SetupOneCellDelegate;

@interface setupOneCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *socialName;
@property (weak, nonatomic) IBOutlet FUISwitch *cellSwitch;

@property (weak, nonatomic) id<SetupOneCellDelegate> delegate;

@end


@protocol SetupOneCellDelegate <NSObject>

- (void)setupOneCell:(setupOneCell *)cell switchValueDidChange:(BOOL)switchValue;

@end

现在您的 View Controller 已全部设置为在其开关值更改时从单元格获取回调。配置单元格时,请确保将 View Controller 设置为委托(delegate):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
setupOneCell *cell = (setupOneCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

cell.cellSwitch.onColor = [UIColor turquoiseColor];
cell.cellSwitch.offColor = [UIColor cloudsColor];
cell.cellSwitch.onBackgroundColor = [UIColor midnightBlueColor];
cell.cellSwitch.offBackgroundColor = [UIColor silverColor];
cell.cellSwitch.offLabel.font = [UIFont boldFlatFontOfSize:14];
cell.cellSwitch.onLabel.font = [UIFont boldFlatFontOfSize:14];

Social *media = (self.medias)[indexPath.row];

cell.socialName.text = media.socialMedia;

cell.delegate = self;

return cell;
}

最后实现委托(delegate)方法:

- (void)setupOneCell:(setupOneCell *)cell switchValueDidChange:(BOOL)switchValue {
NSIndexPath *indexPath = [self.oneTableView indexPathForCell:cell];
NSString *label = [self.medias[indexPath.row] socialMedia];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:switchValue forKey:label];
}

关于ios - 识别方法中的表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24354894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com