Using my own Cell object in XCODE

I successfully created my "cell" object (based on UITableViewCell) and successfully used it when building the table through cellForRowAtIndexPath .

However, how can I deconstruct what I did in didSelectRowAtIndexPath ?

I am currently receiving the error message "Incompatible pointer types initializing" MyCell * __ strong "with an expression like" UITableViewCell "

Given that my object (MyCell) is based on a UITableViewCell, I don’t understand why I am getting this error.

Could you help me understand what I am doing wrong.

My alternative is to use "TAG" for each of the two labels in the cell and get them that way, but I'm just experimenting here, trying to learn more about how it all works.

Any help would be greatly appreciated.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"myCellID"; MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; if (cell == nil) { cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } cell.accountCode.text = @"0009810"; cell.accountName.text = @"Agent Name"; return cell; } 

Here is another method. I get the error MyCell * cell = [tableView cellForRowAtIndexPath: indexPath];

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Get the cell that was selected MyCell *cell = [tableView cellForRowAtIndexPath:indexPath]; AccountRecord *accountRecord = [[AccountRecord alloc] init]; accountRecord.accountCode = cell.accountCode.text; accountRecord.accountName = cell.accountName.text; [self performSegueWithIdentifier:@"AccountDetail" sender:accountRecord]; } 

And here is MyCell

 #import <UIKit/UIKit.h> @interface MyCell : UITableViewCell @property (nonatomic,strong) IBOutlet UILabel *accountCode; @property (nonatomic,strong) IBOutlet UILabel *accountName; @end 

Any help would be appreciated.

+4
source share
2 answers

Change this:

 MyCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

to the next:

 MyCell *cell = (MyCell *) [tableView cellForRowAtIndexPath:indexPath]; 
+8
source

It seems to me that you just need to give UITableViewCell * to MyCell *

 MyCell *cell = (MyCell*)[tableView cellForRowAtIndexPath:indexPath]; 

Everything else looks good to me.

Usually I did not get cell information by calling cellForRowAtIndexPath, instead I would take it from the underlying data model.

+1
source

Source: https://habr.com/ru/post/1441723/


All Articles