Faizan Tanveer
Faizan Tanveer

Reputation: 335

get the row number from table view

i am new in iphone development, i am using table view to list name of the student, what i want to do is i want to get the row number of the cell the user pressed , currently i can get the name(string) of the cell,

//code in didSelectRowAtIndexPath

//the obj.name where is a string, it works fine

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

//if i try obj.num , where num is nsinteger .i am getting errors of type i dnt understand

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

i even tried but i cant get the number. example if faizan lies on 12th cell. what i want to do is,is save 12 in a number and show it on the next view label.i cant put it in the variable thanks for your help in advance

Upvotes: 0

Views: 4293

Answers (4)

Rahul Chavan
Rahul Chavan

Reputation: 510

Use this code to find the selected index.

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

Upvotes: 0

Kjuly
Kjuly

Reputation: 35191

@Trisha & @tarmes both are right. Maybe I can make it clear for you. :)

As the example you gave, I think the obj is your another view's object, and you want to set the name & number of range, right?

In your .h file, don't forget:

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

Then use it in other view controllers:

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  

You can treat NSInteger with int as the same, see NSInteger's declaration below:

#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

Upvotes: 0

tarmes
tarmes

Reputation: 15442

I really don't understand what you're trying to do. Your question is "how to get the row number of the cell the user pressed", which you're already doing:

indexPath.row

As for this very odd code:

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

You appear to be getting the name (as a string) from an array, and then you're trying to "count" the string, which you can't do. You can get it's length:

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

But that's not what you're asking for - what you're asking for you've already used in that expression!

Tim

Upvotes: 3

Aravindhan
Aravindhan

Reputation: 15628

IndidSelectRowAtIndexPath save the row number user pressed

int rowNumber = indexPath.row;

Upvotes: 6

Related Questions