objective-c - 应用在initWithStyle上崩溃

标签 objective-c ios uitableview

我不知道为什么我的应用程序崩溃。这是代码:

-(void)viewDidLoad
{
    [super viewDidLoad];

    self.tableContent = [NSArray arrayWithObjects:@"Blue", @"Yellow", @"Green", nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  [self.tableContent count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    return [self.tableContent objectAtIndex:indexPath.row];

}

这是我得到的错误
-[NSCFString setTableViewStyle:]: unrecognized selector sent to instance 0x4938

最佳答案

这是您的cellForRowAtIndexPath中的错误:

return [self.tableContent objectAtIndex:indexPath.row];
tableContentNSString的数组,因此您将返回NSString而不是UITableViewCell类型的对象。所以写:
return cell;

导致错误的原因是您返回的NSString代替了UITableViewCell ...如果要查看发送的文本,则需要设置textlabel的文本:
cell.textlabel.text = [self.tableContent objectAtIndex:indexPath.row];
//set the text and then return the cell
return cell;

关于objective-c - 应用在initWithStyle上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9530122/

相关文章:

ios - 向 AVAudioEngine 添加输入回调

objective-c - %d 没有正确显示整数

ios - 有人可以解释这个@synthesize 语法吗?

ios - 如何使用十六进制颜色值

ios - 在后台线程上将核心数据图像加载到 UITableViewCell 时崩溃

ios - UITableView 委托(delegate)方法未在 swift 3 中调用

iphone - 在 iOS 应用程序中选择使用 native 语音备忘录应用程序创建的音频文件

ios - 为什么我的时间 UILabels 从暂停状态恢复播放时会跳过 2 秒?

ios - NSString 类别中的扩展属性崩溃

ios - 如何在 iOS 中实现如下图所示的搜索功能?