应用程序崩溃在initWithStyle上

foh*_*oho 1 objective-c uitableview ios

我无法理解我的应用程序崩溃的原因.这是代码:

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

}
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误

-[NSCFString setTableViewStyle:]: unrecognized selector sent to instance 0x4938
Run Code Online (Sandbox Code Playgroud)

tip*_*low 5

这是您的错误cellForRowAtIndexPath:

return [self.tableContent objectAtIndex:indexPath.row];
Run Code Online (Sandbox Code Playgroud)

tableContent是一个NSStrings 数组,所以你要返回一个NSString而不是一个类型的对象UITableViewCell.所以改为写:

return cell;
Run Code Online (Sandbox Code Playgroud)

它是NSString你回来到位的UITableViewCell导致错误...如果你想看到你发送的文字,你需要设置为textLabel的文字:

cell.textlabel.text = [self.tableContent objectAtIndex:indexPath.row];
//set the text and then return the cell
return cell;
Run Code Online (Sandbox Code Playgroud)