【问题标题】:select cell from UITableView and go to the another UITableView从 UITableView 中选择单元格并转到另一个 UITableView
【发布时间】:2012-10-18 12:00:43
【问题描述】:

我在第二个 tableview 时总是出错。我使用情节提要创建了一个简单的应用程序。我的应用程序第一次运行时,它会显示我创建的数据列表。如果用户单击该行,我想更改为另一个页面,在另一个页面上也有一个 tableview。我还硬编码了数据列表,但我没有显示出来。

这部分总是出错

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SubSiteCellDetail"];
    cell.textLabel.text = [ListDetailSubSite objectAtIndex:indexPath.row]; //this always give me the error
    return cell;
}

这是我创建数据列表的方式

ListDetailSubSite = [NSMutableArray arrayWithCapacity:10];
SubSite *detailSubSite = [[SubSite alloc] init];
detailSubSite.sSubSiteName = [subsite.sSubSiteName stringByAppendingString:@"1"];
[ListDetailSubSite addObject:detailSubSite];


detailSubSite = [[SubSite alloc] init];
detailSubSite.sSubSiteName = [subsite.sSubSiteName stringByAppendingString:@"2"];
[ListDetailSubSite addObject:detailSubSite];

这是我如何从 UITableView 移动到另一个 UITableView。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"ShowSubSiteDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        SubSite *subsetSelected = [ListSubSite objectAtIndex:indexPath.row];
        [[segue destinationViewController] setSubsite:subsetSelected];
    }
}

这是我的错误看起来像

2012-10-29 15:18:11.127 UniversalStoryBoard[5256:f803] -[SubSite isEqualToString:]: unrecognized selector sent to instance 0x9355340
2012-10-29 15:18:11.129 UniversalStoryBoard[5256:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SubSite isEqualToString:]: unrecognized selector sent to instance 0x9355340'
*** First throw call stack:
(0x13ce022 0x155fcd6 0x13cfcbd 0x1334ed0 0x1334cb2 0x1580ff 0x4fb1 0xb2c54 0xb33ce 0x9ecbd 0xad6f1 0x56d21 0x13cfe42 0x1d86679 0x1d90579 0x1d154f7 0x1d173f6 0x1d16ad0 0x13a299e 0x1339640 0x13054c6 0x1304d84 0x1304c9b 0x12b77d8 0x12b788a 0x18626 0x1fcd 0x1f35)
terminate called throwing an exception

这是图片:

类 SubSite.h

#import <UIKit/UIKit.h>

@interface SubSite : NSObject
@property (nonatomic, copy) NSString *sSubSiteName;

@end

类 SubSite.m

#import "SubSite.h"

@implementation SubSite
@synthesize sSubSiteName;

@end

谁能帮我解决这个问题?谢谢。

【问题讨论】:

    标签: iphone ios ios5 xcode4.3 uistoryboard


    【解决方案1】:

    您发现的错误是什么? 你试试这个吗

    cell.textLabel.text = [NSString stringWithFormat @"%@",[ListDetailSubSite objectAtIndex:indexPath.row]];
    

    我认为出现问题是因为cell.textlable.text 接受NSString 所以你需要转换你的对象

    你可以这样做

    Subset *tmp = [ListDetailSubSite objectAtIndex:indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@",tmp.sSubSetName];
    

    【讨论】:

    • 嗨,感谢您的回答。几乎解决了,但为什么输出像内存地址?如何从 indexpath.row 中获取文本?谢谢。
    • 是的,SubSite 是一个类。我将添加 SubSite 类。
    • 我改进了我的答案,希望对您有所帮助
    • 嗨,感谢您的回答。我已经解决了。我投票给你的答案。谢谢。
    【解决方案2】:
    cell.textLabel.text = [ListDetailSubSite objectAtIndex:indexPath.row]
    

    应该替换为

    cell.textLabel.text = [[ListDetailSubSite objectAtIndex:indexPath.row] sSubSiteName] 
    

    【讨论】:

    • 嗨,感谢您的回答。但老实说,有什么不同?我的意思是你的答案。
    • 对不起,我犯了一个错误,现在我更正它。希望不要误导你
    • 嗨,没关系..不用担心。我投票给你的答案,因为你的答案也是正确的......谢谢。 :)
    【解决方案3】:

    问题可能是因为cell.textLabel.text = [ListDetailSubSite objectAtIndex:indexPath.row]; 中的单元格需要一个字符串而不是 ListDetailSubSite 对象..

    类似的错误已在此链接中解决:

    Why am I getting an isEqualToString error in this Cocoa code?

    既然您提到您已经创建了一个 Subsite 类,您可以尝试该方法.. 基本上您需要确保cell.textLabel.text 的输入是一个 NSString 对象..

    【讨论】:

    • 嗨,感谢您的回答.. 它真的很有帮助。再次感谢。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多