制作单元格时分组表问题

时间:2012-10-21 00:46:43

标签: iphone ios

这只是让我疯了。我正在尝试制作一个简单的分组表,每当我运行它时,我都会收到此错误:

-[__NSCFConstantString objectAtIndex:]: unrecognized selector sent to instance 0x489c

这是抛出这条线的代码(我把确切的行放在星号中):

-(UITableViewCell *)tableView:(UITableView *)tableView
    cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *newSpecCell = @"newSpecCell";
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [segmentArray objectAtIndex:section];
NSArray *names = [specsDictionary objectForKey:key];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:newSpecCell];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                  reuseIdentifier:newSpecCell];
}

cell.textLabel.text = [names objectAtIndex:row]; // <---
return cell;

}

从我的逻辑中我要求数组中的objectAtIndex,但错误说我将它发送到一个常量字符串。

我尝试过一些事情(比如从[names objectAtIndex:row]创建一个字符串,但似乎没有任何区别。另外,如果我使用NSLog显示names中的值,它会显示它们正确,所以我知道数组包含我需要的值。

1 个答案:

答案 0 :(得分:0)

数组填充错误的简单问题。感谢Maddy,我很快就明白了。

-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *newSpecCell = @"newSpecCell";
 NSUInteger section = [indexPath section];
 // NSUInteger row = [indexPath row];
 NSString *key = [segmentArray objectAtIndex:section];
 // NSArray *names = [specsDictionary objectForKey:key];
 NSString *values = [specsDictionary objectForKey:key]; //<--
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:newSpecCell];
 if (cell == nil) {
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                              reuseIdentifier:newSpecCell];
}

cell.textLabel.text = values; //<--
return cell;
}

我只是删除了注释行并修改了cell.textlabel.text,如图所示,一切都很顺利!

相关问题