iOS - segue and tableView



我是iOS编程的新手。我正在学习一本书和一些教程。我需要一些帮助来理解这些方法

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"PushAppDetails"])
{
AppDetailsViewController *appDetailsViewController = segue.destinationViewController;
UITableViewCell *cell = sender;
appDetailsViewController.appDetails =
[[AppDetails alloc] initWithName:cell.textLabel.text
description:cell.detailTextLabel.text];
}
}

{
//Set the CellIdentifier that you set in the storyboard
static NSString *CellIdentifier = @"AppCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
switch (indexPath.row)
{
case 0:
cell.textLabel.text = @"Awesome App";
cell.detailTextLabel.text = @"Long description of the awesome app...";
break;
case 1:
cell.textLabel.text = @"Even More Awesome App";
cell.detailTextLabel.text = @"Long description of the even more awesome app...";
break;
case 2:
cell.textLabel.text = @"The Most Awesome App Ever";
cell.detailTextLabel.text =
@"Long description of the most awesome app ever seen...";
break;
default:
cell.textLabel.text = @"Unkown";
cell.detailTextLabel.text = @"Unknown";
break;
}
return cell;
}

我不明白的是

UITableViewCell *cell = sender;
appDetailsViewController.appDetails =
[[AppDetails alloc] initWithName:cell.textLabel.text
description:cell.detailTextLabel.text];

我得到了这个消息,我正在从这行[segue.identifier isEqualToString:@"PushAppDetails"]中识别segue,然后我创建了AppdetailsViewController类的对象,但我没有得到这行正在做的

UITableViewCell *cell = sender; 

以及这一行是如何调用底部表函数的,切换函数在哪里,以及每个单元格的描述,而这一行都不是

appDetailsViewController.appDetails =
[[AppDetails alloc] initWithName:cell.textLabel.text
description:cell.detailTextLabel.text];

我的appDetails类中有一个方法。。如果我必须访问方法,为什么不简单地我可以这样做呢

AppDetails *app = new [AppDetails alloc]init
[app initWithName:cell.textLabel.text
description:cell.detailTextLabel.text];

我实际上来自java,所以我觉得有点难以理解这个

UITableViewCell *cell = sender;基本上通过一个变量将sender强制转换为UITableViewCell的实例。此选项用于以后访问cell.textLabelcell.detailTextLabel。由于sender属于id类型,因此无法编写sender.textLabel。但如果你喜欢的话,你可以选择:

appDetailsViewController.appDetails =
[[AppDetails alloc] initWithName:((UITableViewCell *)sender).textLabel.text
description:((UITableViewCell *)sender).detailTextLabel.text];

如果你更喜欢这个,可以随意使用。我个人更喜欢这个作业,而不是特定的类别,因为它更明显。

并且这一行不调用tableView:cellForRowAtIndexPath:,它使用已经在UITableViewCell上设置的NSString。调用prepareForSegue:sender:的苹果程序代码将把被触摸的单元格作为参数sender传递。


AppDetails *app = new [AppDetails alloc]init
[app initWithName:cell.textLabel.text description:cell.detailTextLabel.text];

首先,Objective-C是无效的。如果你用Objective-C编写代码,你不应该期望你可以使用Java语法。

在Objective-c中,您不应该多次调用init,这可能会产生许多奇怪的影响,具体取决于init的实现。

例如,以以下代码为例:

- (id)init {
if (self = [super init]) {
self.label = [[UILabel alloc] init];
[self.view addSubview:self.label];
}
return self;
}
- (id)initWithName:(NSString *)name {
if (self = [self init]) {   // calls init
self.label.text = name;
}
return self;
}

如果您调用init,它将添加一个新的UILabel,如果您稍后调用initWithName:,它将增加另一个UILabel,因为initWithName:本身将调用init
因此,如果您先调用init,然后再调用initWithName:,则最终会得到两个UILabels
由于您不知道正在调用的大多数init方法的实现细节,因此在Objective-C中不应该多次调用init。

CCD_ 19应该始终是CCD_ 20的一部分。

最新更新