gpt4 book ai didi

ios - 选择一个单元格显示一个按钮,取消选择隐藏该按钮

转载 作者:行者123 更新时间:2023-11-29 02:28:55 24 4
gpt4 key购买 nike

我希望在我选择一个单元格时显示一个按钮,当我选择同一个单元格或另一个单元格时我希望该按钮隐藏。因此一次只能显示一个按钮。

我有什么:

在我的 .h 文件中:

@property (strong, nonatomic) NSIndexPath *isselectednow;

和我的 .m 文件:

查看加载:

self.isselectednow = [NSIndexPath indexPathForRow:7 inSection:1];

cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}


//[cell addSubview:[self drawSeparationView:(indexPath.row)]];

UILabel *DateName = (UILabel *)[cell viewWithTag:100100];
DateName.textColor = [UIColor blackColor];
//DateName.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row ];
DateName.text = objects[indexPath.row];

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(buttonOnCellPressed:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Update" forState:UIControlStateNormal];
button.frame = CGRectMake(DEVICE_SIZE.width - 60, 18, 55, 15);

[cell addSubview:button];
if ([indexPath compare:self.isselectednow] == NSOrderedSame) {
button.hidden = NO;
}
else{
button.hidden = YES;
}

UILabel *lbl1 = (UILabel *)[cell viewWithTag:100101];
lbl1.textColor = UIColorFromRGB(0x141414);
UILabel *lbl2 = (UILabel *)[cell viewWithTag:100102];
lbl2.textColor = UIColorFromRGB(0x141414);
UILabel *lbl3 = (UILabel *)[cell viewWithTag:100103];
lbl3.textColor = UIColorFromRGB(0x141414);
UILabel *lbl4 = (UILabel *)[cell viewWithTag:100104];
lbl4.textColor = UIColorFromRGB(0x141414);
UILabel *lbl5 = (UILabel *)[cell viewWithTag:100105];
lbl5.textColor = UIColorFromRGB(0x141414);

UITextField *breakfastName = (UITextField *) [cell viewWithTag:100200];
breakfastName.attributedPlaceholder =
[[NSAttributedString alloc]
initWithString:@"Name here"
attributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0x141414)}];
breakfastName.textColor = UIColorFromRGB(0x141414);
[breakfastName setDelegate:self];
//[breakfastName addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];

UITextField *OutName = (UITextField *) [cell viewWithTag:100201];
OutName.attributedPlaceholder =
[[NSAttributedString alloc]
initWithString:@"Name here"
attributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0x141414)}];
OutName.textColor = UIColorFromRGB(0x141414);
[OutName setDelegate:self];

UITextField *lunchName = (UITextField *) [cell viewWithTag:100202];
lunchName.attributedPlaceholder =
[[NSAttributedString alloc]
initWithString:@"Name here"
attributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0x141414)}];
lunchName.textColor = UIColorFromRGB(0x141414);
[lunchName setDelegate:self];

UITextField *InName = (UITextField *) [cell viewWithTag:100203];
InName.attributedPlaceholder =
[[NSAttributedString alloc]
initWithString:@"Name here"
attributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0x141414)}];
InName.textColor = UIColorFromRGB(0x141414);
[InName setDelegate:self];

UITextField *eveningName = (UITextField *) [cell viewWithTag:100204];
eveningName.attributedPlaceholder =
[[NSAttributedString alloc]
initWithString:@"Name here"
attributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0x141414)}];
eveningName.textColor = UIColorFromRGB(0x141414);
[eveningName setDelegate:self];

UITextField *othersName = (UITextField *) [cell viewWithTag:100205];
othersName.attributedPlaceholder =
[[NSAttributedString alloc]
initWithString:@"Name here"
attributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0x141414)}];
othersName.textColor = UIColorFromRGB(0x141414);



cell.clipsToBounds = YES;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.backgroundColor = UIColorFromRGB(0xf3f0e9);

返回单元格;

didSelectRowAtIndexPath:

[tableView beginUpdates];
if ([indexPath compare:self.isselectednow] == NSOrderedSame) {
self.isselectednow = nil;
}
else{
self.isselectednow = indexPath;
}

[tableView endUpdates];
//[tableView reloadData];

enter image description here

最佳答案

来自docs about beginUpdates :

Call this method if you want subsequent insertions, deletion, and selection operations (for example, cellForRowAtIndexPath: and indexPathsForVisibleRows) to be animated simultaneously.

尽管您已经更改了 isSelectedNow bool 值,您实际上没有在 beginUpdates 之间指定任何插入、删除或选择操作和 endUpdates .因此,您实际上并没有使用该代码开始或结束对单元格内容的任何更新。

我不确定你想做什么,但取出 [tableView beginUpdates];[tableView beginUpdates];并重新插入 [tableView reloadData];将导致正确的数据重新加载。

编辑:

除此之外,问题是您正在使用 dequeueReusableCellWithIdentifier: 重复使用您的单元格,但您要添加一个新的 UIButton subview 每次,所以你基本上把每个新的 UIButton在旧的。我建议按照您在 cellForRowAtIndexPath: 中对所有标签和文本字段所做的操作进行操作-- 通过将更新按钮添加到您的自定义表格 View 单元格并使用 viewWithTag: 访问它来重复使用您的更新按钮.

关于ios - 选择一个单元格显示一个按钮,取消选择隐藏该按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27158921/

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