如何管理 iOS 中的自定义单元格标签值?
How to manage custom cell label value in iOS?
我正在构建一个 iOS 应用程序 storyboards.I 使用了 UITableViewController,它有 6 个自定义单元格,每个单元格包含三个 IBOutlet 按钮和一个 IBOutlet 标签。
当用户单击一个特定自定义单元格中的任何按钮时,只有该特定单元格标签的值应该改变。
但实际情况是,每个自定义单元格中所有标签的值都发生了变化。
这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *Cellidentifier1 = @"List";
Cell *cell1 = [tableView dequeueReusableCellWithIdentifier:Cellidentifier1 forIndexPath:indexPath];
cell1.selectionStyle = UITableViewCellSelectionStyleNone;
// Configure the cell...
long row = [indexPath row];
cell1.First.tag=row;//button iboutlet in cell
cell1.Second.tag=row;//button iboutlet in cell
cell1.Third.tag=row;//button iboutlet in cell
cell1.Level.tag=row;
cell1.Level.text=Skill;//label iboutlet in cell
return cell1;
}
-(IBAction)FirstAction:(id)sender{
Skill=@"first";
[self.tableView reloadData];
}
-(IBAction)SecondAction:(id)sender{
Skill=@"Second";
[self.tableView reloadData];
}
-(IBAction)ThirdAction:(id)sender{
Skill=@"Third";
[self.tableView reloadData];
}
首先你要做的是在选择行时使用委托方法selectRowAtIndexPath
然后可以使用indexPath选择行按钮...
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
}
选中该行后,将执行所需的行按钮功能
试试下面的代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *Cellidentifier1 = @"List";
Cell *cell1 = [tableView dequeueReusableCellWithIdentifier:Cellidentifier1 forIndexPath:indexPath];
cell1.selectionStyle = UITableViewCellSelectionStyleNone;
// Configure the cell...
long row = [indexPath row];
cell1.First.tag=row;//button iboutlet in cell
cell1.Second.tag=row;//button iboutlet in cell
cell1.Third.tag=row;//button iboutlet in cell
cell1.Level.tag=row;
cell1.Level.text=Skill;//label iboutlet in cell
[cell1.First addTarget:self action:@selector(FirstAction:) forControlEvents:UIControlEventTouchUpInside];
[cell1.Second addTarget:self action:@selector(SecondAction:) forControlEvents:UIControlEventTouchUpInside];
[cell1.Third addTarget:self action:@selector(ThirdAction:) forControlEvents:UIControlEventTouchUpInside];
return cell1;
}
有几种方法可以做到这一点。但是,通过查看您的代码,我会确保您从所选单元格中获得正确的 indexPath。假设您已经设置了目标操作,您可以像这样重写 IBAction 方法:
-(IBAction)firstAction:(UIButton *)sender{
int row = sender.tag;
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:row inSection:0];
Cell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
Skill = @"first";
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
这将获得正确的单元格和索引路径。仅更新该单元格的标签,然后重新加载它。
几个问题:
您应该有一个模型来反映您的 table 视图中的内容。具体来说,现在您只有一个 Skill
值,但是您有六行。您想要维护一个模型,该模型是一个值数组,例如:
@property (nonatomic, strong) NSMutableArray *values;
和
- (void)viewDidLoad {
[super viewDidLoad];
self.values = [@[@"first", @"first", @"first", @"first", @"first", @"first"] mutableCopy];
}
和
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *Cellidentifier1 = @"List";
Cell *cell = [tableView dequeueReusableCellWithIdentifier:Cellidentifier1 forIndexPath:indexPath];
NSString *value = self.values[indexPath.row];
cell.level.text = value;
return cell;
}
注意,不需要标签号。
当您点击按钮时,您必须 (a) 确定 table 中对应的行; (b) 更新模型中的那一行;和 (c) 重新加载 table 中的那一行(这将在模型中查找值):
- (void)updateCell:(UIView *)cell withValue:(NSString *)value {
NSParameterAssert(cell);
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)cell];
if (cell) {
self.values[indexPath.row] = value;
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
- (IBAction)didTapFirstButton:(id)sender {
[self updateCell:[[sender superview] superview] withValue:@"first"];
}
- (IBAction)didTapSecondButton:(id)sender{
[self updateCell:[[sender superview] superview] withValue:@"second"];
}
- (IBAction)didTapThirdButton:(id)sender{
[self updateCell:[[sender superview] superview] withValue:@"third"];
}
注意,sender
是按钮。因此,我通过抓取 superview
(单元格的内容视图)然后抓取其 superview
(单元格本身)来获取 table 视图单元格。如果您的视图层次结构不同,请适当更改它,但希望这能说明这个想法。
在您的 IBOutlet 方法中添加它以查找行的索引路径
CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint];
而不是重新加载整个表格视图,您只想重新加载指定单元格的文本 indexPath
要仅重新加载一个单元格,请执行此操作而不是 [self.tableView reloadData];
[self.tableView reloadRowsAtIndexPaths:@[hitIndex] withRowAnimation:UITableViewRowAnimationNone];
我正在构建一个 iOS 应用程序 storyboards.I 使用了 UITableViewController,它有 6 个自定义单元格,每个单元格包含三个 IBOutlet 按钮和一个 IBOutlet 标签。
当用户单击一个特定自定义单元格中的任何按钮时,只有该特定单元格标签的值应该改变。
但实际情况是,每个自定义单元格中所有标签的值都发生了变化。
这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *Cellidentifier1 = @"List";
Cell *cell1 = [tableView dequeueReusableCellWithIdentifier:Cellidentifier1 forIndexPath:indexPath];
cell1.selectionStyle = UITableViewCellSelectionStyleNone;
// Configure the cell...
long row = [indexPath row];
cell1.First.tag=row;//button iboutlet in cell
cell1.Second.tag=row;//button iboutlet in cell
cell1.Third.tag=row;//button iboutlet in cell
cell1.Level.tag=row;
cell1.Level.text=Skill;//label iboutlet in cell
return cell1;
}
-(IBAction)FirstAction:(id)sender{
Skill=@"first";
[self.tableView reloadData];
}
-(IBAction)SecondAction:(id)sender{
Skill=@"Second";
[self.tableView reloadData];
}
-(IBAction)ThirdAction:(id)sender{
Skill=@"Third";
[self.tableView reloadData];
}
首先你要做的是在选择行时使用委托方法selectRowAtIndexPath
然后可以使用indexPath选择行按钮...
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
}
选中该行后,将执行所需的行按钮功能
试试下面的代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *Cellidentifier1 = @"List";
Cell *cell1 = [tableView dequeueReusableCellWithIdentifier:Cellidentifier1 forIndexPath:indexPath];
cell1.selectionStyle = UITableViewCellSelectionStyleNone;
// Configure the cell...
long row = [indexPath row];
cell1.First.tag=row;//button iboutlet in cell
cell1.Second.tag=row;//button iboutlet in cell
cell1.Third.tag=row;//button iboutlet in cell
cell1.Level.tag=row;
cell1.Level.text=Skill;//label iboutlet in cell
[cell1.First addTarget:self action:@selector(FirstAction:) forControlEvents:UIControlEventTouchUpInside];
[cell1.Second addTarget:self action:@selector(SecondAction:) forControlEvents:UIControlEventTouchUpInside];
[cell1.Third addTarget:self action:@selector(ThirdAction:) forControlEvents:UIControlEventTouchUpInside];
return cell1;
}
有几种方法可以做到这一点。但是,通过查看您的代码,我会确保您从所选单元格中获得正确的 indexPath。假设您已经设置了目标操作,您可以像这样重写 IBAction 方法:
-(IBAction)firstAction:(UIButton *)sender{
int row = sender.tag;
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:row inSection:0];
Cell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
Skill = @"first";
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
这将获得正确的单元格和索引路径。仅更新该单元格的标签,然后重新加载它。
几个问题:
您应该有一个模型来反映您的 table 视图中的内容。具体来说,现在您只有一个
Skill
值,但是您有六行。您想要维护一个模型,该模型是一个值数组,例如:@property (nonatomic, strong) NSMutableArray *values;
和
- (void)viewDidLoad { [super viewDidLoad]; self.values = [@[@"first", @"first", @"first", @"first", @"first", @"first"] mutableCopy]; }
和
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *Cellidentifier1 = @"List"; Cell *cell = [tableView dequeueReusableCellWithIdentifier:Cellidentifier1 forIndexPath:indexPath]; NSString *value = self.values[indexPath.row]; cell.level.text = value; return cell; }
注意,不需要标签号。
当您点击按钮时,您必须 (a) 确定 table 中对应的行; (b) 更新模型中的那一行;和 (c) 重新加载 table 中的那一行(这将在模型中查找值):
- (void)updateCell:(UIView *)cell withValue:(NSString *)value { NSParameterAssert(cell); NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)cell]; if (cell) { self.values[indexPath.row] = value; [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } } - (IBAction)didTapFirstButton:(id)sender { [self updateCell:[[sender superview] superview] withValue:@"first"]; } - (IBAction)didTapSecondButton:(id)sender{ [self updateCell:[[sender superview] superview] withValue:@"second"]; } - (IBAction)didTapThirdButton:(id)sender{ [self updateCell:[[sender superview] superview] withValue:@"third"]; }
注意,
sender
是按钮。因此,我通过抓取superview
(单元格的内容视图)然后抓取其superview
(单元格本身)来获取 table 视图单元格。如果您的视图层次结构不同,请适当更改它,但希望这能说明这个想法。
在您的 IBOutlet 方法中添加它以查找行的索引路径
CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint];
而不是重新加载整个表格视图,您只想重新加载指定单元格的文本 indexPath
要仅重新加载一个单元格,请执行此操作而不是 [self.tableView reloadData];
[self.tableView reloadRowsAtIndexPaths:@[hitIndex] withRowAnimation:UITableViewRowAnimationNone];