我怎样才能让 Table 单元执行两个不同的 segue 但有条件?
How can I have a Table Cell to perform two different segues but with conditions?
我是 iOS 开发的新手,所以请原谅我提出愚蠢的问题或做出愚蠢的行为。
现在,问题来了。我正在开发一个应用程序,我有一个标签栏控制器作为初始,称为 [Initial Tab View]。其中一个选项卡是显示所有项目的 table 视图,我们称之为 [Item View]。一旦用户点击单元格,它将推送到另一个视图并显示详细信息,即 [Detail View]。另一个选项卡也是 table 视图,但带有静态单元格 [Static View],我用它来 select 一个项目和 return.
更清晰的图片:如果我从初始选项卡视图访问[项目视图],只需简单地按照[初始选项卡视图]->[项目视图]->[详细视图]。但是,如果我先进入[Static View],然后进入[Item View],过程就会像[Initial Tab View]->[Static View]->[Item View]->[Static View]。
我不知道如何在 [Item View] 中实现第二个(使用 [Static View] ),有人可以帮忙吗?还是有另一种更好的方法?非常感谢。
// Code from [Item View]
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"CustomShopCell";
ShopCustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[ShopCustomTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:identifier];
}
if (searchResult != nil) {
//cell.textLabel.text = [searchResult objectAtIndex:indexPath.row];
Shop *shop = searchResult[indexPath.row];
if ([shop.shopNameChi length] == 0) {
cell.lblName.text = shop.shopName;
} else {
cell.lblName.text = [NSString stringWithFormat:@"%@ | %@", shop.shopName, shop.shopNameChi];
}
cell.lblLocation.text = shop.unit;
cell.imgImage.contentMode = UIViewContentModeScaleAspectFit;
cell.imgImage.image = [UIImage imageWithData:shop.imageData];
} else {
//cell.textLabel.text = [list objectAtIndex:indexPath.row];
Shop *shop = shopList[indexPath.row];
if ([shop.shopNameChi length] == 0) {
cell.lblName.text = shop.shopName;
} else {
cell.lblName.text = [NSString stringWithFormat:@"%@ | %@", shop.shopName, shop.shopNameChi];
}
cell.lblLocation.text = shop.unit;
cell.imgImage.contentMode = UIViewContentModeScaleAspectFit;
cell.imgImage.image = [UIImage imageWithData:shop.imageData];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(![self isKindOfClass:[NavigationMainTableViewController class]]){
[self performSegueWithIdentifier:@"showShopDetail" sender:self];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
尝试使用这个。您可以为您点击的每个单元格设置该方法。
[self performSegueWithIdentifier:@"yourIdentifierHere" sender:self];
您可以做的(如果我确定我理解您在做什么)创建两个具有不同标识符的 segue 链接(名称清晰,我喜欢这种格式 "fromHereToThere")。然后在您的 didselect 中,您可以使用 if 语句调用您的 segue,如下所示:
if (your condition){
[self performSegueWithIdentifier:@"fromHomeToStatic" sender:self];
}else{
[self performSegueWithIdentifier:@"fromHomeToDetail" sender:self];
}
然后在你的 prepareForSegue
方法中(每个控制器默认在注释中的方法 class)你可以选择要传递的数据,
if ([segue.identifier isEqualToString:@"fromHomeToDetail"])
{
//Manage your usual stuff here for the detail identifier
//Ask me again if you need extra help with that
}
if ([segue.identifier isEqualToString:@"fromHomeToStatic"]){
//Manage here for the static identifier
}
但我不太确定你的问题是什么意思,所以我可能完全跑题了:D
编辑:好吧,当你加载第三个控制器时,显然你想知道你从哪里来。所以这里有一种方法可以做到这一点。
在您的目标控制器中,将其添加到 .h
@property (nonatomic) BOOL fromStatic;
(或者你真正想要的任何东西)
然后在你的原始控制器中,在prepareForSegue
中,如果你来自静态,将下一个控制器的BOOL
设置为YES
,如果不是,则设置它NO
,像这样
DetailViewController *dvc = segue.destionationViewController;
dvc.fromStatic = YES; //if you're writing this from the static controller
在目标控制器的 viewDidLoad 中,只需检查它是 YES 还是 NO 并做出相应的反应。
我做对了吗?还是我仍然对您的问题感到困惑?我还是不完全确定你在问什么哈哈:D
如果您使用的是静态单元格原型。只需从单元格拖动到要推送的视图,然后 select 推送 segue 。如果您使用的是动态 UITableViewCell。你可以在 didSelectRowAtIndex 方法中执行 Segue 。
我是 iOS 开发的新手,所以请原谅我提出愚蠢的问题或做出愚蠢的行为。
现在,问题来了。我正在开发一个应用程序,我有一个标签栏控制器作为初始,称为 [Initial Tab View]。其中一个选项卡是显示所有项目的 table 视图,我们称之为 [Item View]。一旦用户点击单元格,它将推送到另一个视图并显示详细信息,即 [Detail View]。另一个选项卡也是 table 视图,但带有静态单元格 [Static View],我用它来 select 一个项目和 return.
更清晰的图片:如果我从初始选项卡视图访问[项目视图],只需简单地按照[初始选项卡视图]->[项目视图]->[详细视图]。但是,如果我先进入[Static View],然后进入[Item View],过程就会像[Initial Tab View]->[Static View]->[Item View]->[Static View]。
我不知道如何在 [Item View] 中实现第二个(使用 [Static View] ),有人可以帮忙吗?还是有另一种更好的方法?非常感谢。
// Code from [Item View]
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"CustomShopCell";
ShopCustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[ShopCustomTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:identifier];
}
if (searchResult != nil) {
//cell.textLabel.text = [searchResult objectAtIndex:indexPath.row];
Shop *shop = searchResult[indexPath.row];
if ([shop.shopNameChi length] == 0) {
cell.lblName.text = shop.shopName;
} else {
cell.lblName.text = [NSString stringWithFormat:@"%@ | %@", shop.shopName, shop.shopNameChi];
}
cell.lblLocation.text = shop.unit;
cell.imgImage.contentMode = UIViewContentModeScaleAspectFit;
cell.imgImage.image = [UIImage imageWithData:shop.imageData];
} else {
//cell.textLabel.text = [list objectAtIndex:indexPath.row];
Shop *shop = shopList[indexPath.row];
if ([shop.shopNameChi length] == 0) {
cell.lblName.text = shop.shopName;
} else {
cell.lblName.text = [NSString stringWithFormat:@"%@ | %@", shop.shopName, shop.shopNameChi];
}
cell.lblLocation.text = shop.unit;
cell.imgImage.contentMode = UIViewContentModeScaleAspectFit;
cell.imgImage.image = [UIImage imageWithData:shop.imageData];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(![self isKindOfClass:[NavigationMainTableViewController class]]){
[self performSegueWithIdentifier:@"showShopDetail" sender:self];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
尝试使用这个。您可以为您点击的每个单元格设置该方法。
[self performSegueWithIdentifier:@"yourIdentifierHere" sender:self];
您可以做的(如果我确定我理解您在做什么)创建两个具有不同标识符的 segue 链接(名称清晰,我喜欢这种格式 "fromHereToThere")。然后在您的 didselect 中,您可以使用 if 语句调用您的 segue,如下所示:
if (your condition){
[self performSegueWithIdentifier:@"fromHomeToStatic" sender:self];
}else{
[self performSegueWithIdentifier:@"fromHomeToDetail" sender:self];
}
然后在你的 prepareForSegue
方法中(每个控制器默认在注释中的方法 class)你可以选择要传递的数据,
if ([segue.identifier isEqualToString:@"fromHomeToDetail"])
{
//Manage your usual stuff here for the detail identifier
//Ask me again if you need extra help with that
}
if ([segue.identifier isEqualToString:@"fromHomeToStatic"]){
//Manage here for the static identifier
}
但我不太确定你的问题是什么意思,所以我可能完全跑题了:D
编辑:好吧,当你加载第三个控制器时,显然你想知道你从哪里来。所以这里有一种方法可以做到这一点。
在您的目标控制器中,将其添加到 .h
@property (nonatomic) BOOL fromStatic;
(或者你真正想要的任何东西)
然后在你的原始控制器中,在prepareForSegue
中,如果你来自静态,将下一个控制器的BOOL
设置为YES
,如果不是,则设置它NO
,像这样
DetailViewController *dvc = segue.destionationViewController;
dvc.fromStatic = YES; //if you're writing this from the static controller
在目标控制器的 viewDidLoad 中,只需检查它是 YES 还是 NO 并做出相应的反应。
我做对了吗?还是我仍然对您的问题感到困惑?我还是不完全确定你在问什么哈哈:D
如果您使用的是静态单元格原型。只需从单元格拖动到要推送的视图,然后 select 推送 segue 。如果您使用的是动态 UITableViewCell。你可以在 didSelectRowAtIndex 方法中执行 Segue 。