从表视图中获取行号

时间:2011-11-26 09:45:33

标签: iphone objective-c

我是iphone开发的新手,我使用表格视图列出学生的名字,我想要做的是我想得到用户按下的单元格的行号,目前我可以得到名字(字符串) )细胞,

// didSelectRowAtIndexPath中的代码

// obj.name where是一个字符串,它工作正常

obj.name=[obj.studentname objectAtIndex:indexPath.row];

//如果我尝试obj.num,其中num是nsinteger。我得到类型的错误我不明白

obj.num=[[obj.studentname objectAtIndex:indexPath.row]count];
我甚至试过了  但我不能得到这个号码。例如,如果faizan位于第12个细胞上。我想要做的是,在一个数字中保存12并在下一个视图标签上显示它。我不能把它放在变量中 感谢您的帮助

4 个答案:

答案 0 :(得分:6)

didSelectRowAtIndexPath保存按行号的用户

int rowNumber = indexPath.row;

答案 1 :(得分:3)

我真的不明白你想做什么。您的问题是“如何获取用户按下的单元格的行号”,您已经在做了:

indexPath.row

至于这个非常奇怪的代码:

obj.num=[[obj.studentname objectAtIndex:indexPath.row]count];

您似乎从数组中获取名称(作为字符串),然后您尝试“计算”字符串,这是您无法做到的。你可以得到它的长度:

[[obj.studentname objectAtIndex:indexPath.row]length];

但那不是你所要求的 - 你要求的东西已经用在那个表达中了!

答案 2 :(得分:0)

@Trisha& @tarmes都是对的。也许我可以为你说清楚。 :)

正如您给出的示例,我认为obj是您的另一个视图的对象,并且您想要设置名称&范围的数量,对吧?

.h文件中,不要忘记:

@property (nonatomic, copy) NSString * name;
@property (nonatomic, assign) NSInteger num;

然后在其他视图控制器中使用它:

obj.name=[obj.studentname objectAtIndex:indexPath.row];
obj.num =[indexPath row] + 1; // if faizan lies on 12th cell, it saved 12([index cell]=11) in obj.num  

您可以使用int处理NSInteger,请参阅下面的NSInteger声明:

#if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
    typedef long NSInteger;
    typedef unsigned long NSUInteger;
#else
    typedef int NSInteger;
    typedef unsigned int NSUInteger;
#endif

答案 3 :(得分:0)

使用此代码查找所选索引。

  - (void)didSelectObject:(id)object atIndexPath:(NSIndexPath*)indexPath 
    {
        int idx=indexPath.row;
        NSLog(@"Selected cell index:%i",idx);
    }