Objective c 如何在iOS中标记和取消标记行
我有一个表视图,包含通过脚本创建的静态组。我想使用复选标记来更改设置或选择不同的行,但我不知道该怎么做,以编程方式或通过xcode来检查和取消选中行 你能帮帮我吗Objective c 如何在iOS中标记和取消标记行,objective-c,ios,xcode,uitableview,storyboard,Objective C,Ios,Xcode,Uitableview,Storyboard,我有一个表视图,包含通过脚本创建的静态组。我想使用复选标记来更改设置或选择不同的行,但我不知道该怎么做,以编程方式或通过xcode来检查和取消选中行 你能帮帮我吗 提前谢谢 您可以更改UITableViewCell 至于所选内容,您可以在tableView中使用上述属性:didSelectRowAtIndexPath。对于法语/英语部分,我假定您需要在默认情况下选中一个属性。 在您的cellForRowAtIndexPath中: if([cell.textLabel.text isEqualTo
提前谢谢 您可以更改UITableViewCell
至于所选内容,您可以在tableView中使用上述属性:didSelectRowAtIndexPath。对于法语/英语部分,我假定您需要在默认情况下选中一个属性。 在您的cellForRowAtIndexPath中:
if([cell.textLabel.text isEqualToString:@"English"])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
然后,无论何时点击该单元格,您都需要确定其中一个单元格已被点击,然后取消选中另一个单元格
在您的didSelectRowAtIndexPath中
if(indexPath.section ==0 && indexPath.row ==0) {
UITableViewCell * otherCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
otherCell.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else if(indexPath.section ==0 && indexPath.row ==1) {
UITableViewCell * otherCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
otherCell.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
您可以使用标记行或取消标记行
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.accessoryType = UITableViewCellAccessoryNone;
您可以在didSelectRowAtIndexPath中添加上述方法,管理数组中的所有检查并重新加载数据 您可以选中或取消选中视图,这很好,但是您将模型保存在哪里?您使用什么属性来保存有关所选语言的信息 我建议您使用某种枚举数据类型来保存所选语言,并将其包含在
.h
文件中:
enum language {english, french};
@interface TesterProjectViewController : UIViewController
{
enum language selectedLanguage;
}
接下来,我将在您的viewDidLoad:
上选择一种语言作为默认语言,或者使用NSUserDefaults
选择他们上次选择的语言
- (void)viewDidLoad
{
[super viewDidLoad];
self->selectedLanguage = english;
}
然后,我将使用我的UITableViewDelegate
和UITableViewDataSource
函数来了解需要选择哪种语言,以及需要哪个UITableViewCell
具有附件视图:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section==0)
{
switch (indexPath.row) {
case 0:
self->selectedLanguage = english;
break;
case 1:
self->selectedLanguage = french;
break;
default:
break;
}
}
[theTableView reloadData];
}
didselectrowatinexpath:
方法用于设置您在模型中使用的语言。然后使用cellforrowatinexpath:
方法将视图设置为与模型匹配。您可能希望继续并在DIDSelectRowatineXpath:
中设置复选标记,但我更愿意将其保留在单元格中,以便在重新加载数据时始终正确,如:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *theCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
// You may set up the cell differently or do whatever setup you need here:
if(indexPath.section==0)
{
if(self->selectedLanguage == english && indexPath.row==0)
theCell.accessoryType = UITableViewCellAccessoryCheckmark;
else if(self->selectedLanguage == french && indexPath.row==1)
theCell.accessoryType = UITableViewCellAccessoryCheckmark;
else
theCell.accessoryType = UITableViewCellAccessoryNone;
}
//More cell setup can go here, or setup cells for different rows/sections.
return theCell;
}
另外,正如其他人所指出的,我将使用底部的通知选择UISwitch
,因为它们不是“无线电”类型选择,而是每个选择都是开/关类型选择
我希望这对你有帮助 使用以下代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
if (thisCell.accessoryType == UITableViewCellAccessoryNone) {
thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
thisCell.accessoryType = UITableViewCellAccessoryNone;
}
}
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath
*)indexPath {
//add your own code to set the cell accesory type.
return UITableViewCellAccessoryNone;
}
这种设计对于内部部署当然是可以的,但是如果你打算上传到App Store,考虑用个人代码< UISwitc> /Cord>对象替换附件视图,例如,在设置app中使用的方式。如果你想为你的应用程序设置一个设置屏幕,你也可以使用。我认为底部应该使用UISwitch
来关闭和打开不同的通知设置,但我认为复选标记附件可能是处理顶部的最好方法,您只需要选择一种语言或另一种语言。谢谢,但我使用的是静态表,当我使用您的上一种方法时,我的标签不会出现,有没有办法通过interface builder实现这一点?我不知道它们是静态的。但是,它可能不起作用,因为您没有更改reuseIdentifier。我刚刚选择了“cell”,但您需要输入您设置为单元格标识符的任何内容。但是我正在通过接口生成器使用静态组。我可以在代码中使用此属性吗?您已经构建了接口,但需要对执行操作的方法(DidSelectRowatineXpath)进行编码。哇,忘记方法中没有传递单元格。在DidSelectRowatineXpath中的代码之前,加载项:UITableViewCell*cell=[tableview CellForRowatineXpath:indexPath];