Reputation: 3015
I am new to iOS programming. I am following a book and some tutorials to learn it. I need some help in understanding these methods
- (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;
}
What I am not understanding here is these lines
UITableViewCell *cell = sender;
appDetailsViewController.appDetails =
[[AppDetails alloc] initWithName:cell.textLabel.text
description:cell.detailTextLabel.text];
I got this that I am identifying the segue from this line[segue.identifier isEqualToString:@"PushAppDetails"]
and then I created the object of AppdetailsViewController class but i didn't get what this line is doing
UITableViewCell *cell = sender;
and how this line is calling the bottom table function where the switch function is and description of each cell and neither this line
appDetailsViewController.appDetails =
[[AppDetails alloc] initWithName:cell.textLabel.text
description:cell.detailTextLabel.text];
i have a method in my appDetails Class .. why not simply i can do this if i have to access that method
AppDetails *app = new [AppDetails alloc]init
[app initWithName:cell.textLabel.text
description:cell.detailTextLabel.text];
I actually come from java so i feel a little difficult to understand this
Upvotes: 0
Views: 177
Reputation: 90127
UITableViewCell *cell = sender;
basically casts sender
via a variable to an instance of UITableViewCell
. This is used so you can access cell.textLabel
and cell.detailTextLabel
later. Since sender
is of type id
you can't write sender.textLabel
. But you could cast in place if you like:
appDetailsViewController.appDetails =
[[AppDetails alloc] initWithName:((UITableViewCell *)sender).textLabel.text
description:((UITableViewCell *)sender).detailTextLabel.text];
If you like that more feel free to use it. I personally prefer the assignment to the specific class, because it's more obvious.
and this line does not call tableView:cellForRowAtIndexPath:
, it takes the NSStrings that are already set on the UITableViewCell. Apples code that calls prepareForSegue:sender:
will pass the touched cell as parameter sender
.
AppDetails *app = new [AppDetails alloc]init
[app initWithName:cell.textLabel.text description:cell.detailTextLabel.text];
First of all that's not valid Objective-C. If you write code in Objective-C you should not expect that you can use Java syntax.
And in Objective-c you should not call init more than once, depending on the implementation of init this can have many weird effects.
For example take this code:
- (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;
}
If you call init
it will add a new UILabel, and if you call initWithName:
later, it will add another UILabel, because initWithName:
will call init
itself.
So if you call init
first and then initWithName:
you end up with two UILabels.
Since you don't know the implementation details of most init methods you are calling you should never call init more than once in Objective-C.
init...
should always be a part of [[Object alloc] init...
.
Upvotes: 1