4

我已经成功创建了我的“单元格”对象(基于 UITableViewCell),并在通过cellForRowAtIndexPath构建表格时成功使用了它。

但是,我如何解构我在didSelectRowAtIndexPath中所做的事情?

我目前收到错误“使用 'UITableViewCell' 类型的表达式初始化 'MyCell *__strong' 的不兼容指针类型”

鉴于我的对象(MyCell)基于 UITableViewCell,我不明白为什么会出现此错误。

你能帮我理解我做错了什么吗?

我的替代方法是为单元格中的两个标签中的每一个都使用一个“标签”并以这种方式获取它们,但我只是在这里进行试验,试图了解更多关于这一切是如何工作的。

任何帮助将不胜感激。

- (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;
}

这是另一种方法。我在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];

}

这是MyCell

#import <UIKit/UIKit.h>

@interface MyCell : UITableViewCell

@property (nonatomic,strong) IBOutlet UILabel *accountCode;
@property (nonatomic,strong) IBOutlet UILabel *accountName;

@end

任何帮助,将不胜感激。

4

2 回答 2

8

改变这个:

MyCell *cell = [tableView cellForRowAtIndexPath:indexPath];

到以下:

MyCell *cell = (MyCell *) [tableView cellForRowAtIndexPath:indexPath];
于 2012-10-24T04:42:36.827 回答
1

我认为您只需要将 UITableViewCell* 转换为 MyCell*

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

在我看来,其他一切都很好。

通常我不会通过调用 cellForRowAtIndexPath 来获取单元格信息,而是从底层数据模型中获取它。

于 2012-10-24T05:08:44.770 回答