-2

I want to get cell value in tableView by using:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

UITableViewCell *selectedCell=[tableView cellForRowAtIndexPath:indexPath];
NSLog(@"selected cell: %@", selectedCell);

}

It should be a String but this is what i get in the NSLog:

selected cell: <RewardCategoriesTableViewCell: 0x7ae11290; baseClass = UITableViewCell; frame = (0 92.01; 320 44); autoresize = W; layer = <CALayer: 0x7ae11450>>

And this is my Data Source of Array:

NSArray* data= [[NSArray alloc]initWithObjects:category1, category2, category3, category4, category5, category6, category7, nil];
return data;

This is cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

RewardCategoriesTableViewCell *cell = (RewardCategoriesTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"RewardCategoriesTableViewCell"];

if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"RewardCategoriesTableViewCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}

if(indexPath.section >= [categories count]){
    return nil;
}

Categories *category = [categories objectAtIndex:indexPath.section];

NSString *identifier = [NSString stringWithFormat:@"Cell%ld", indexPath.section];

NSLog(@"%@" , identifier);

cell.lblCategory.text = category.CategoryName;

NSLog(@"cell value: %@", cell.lblCategory.text);

return cell;
}

Thanks for helps.

Umit Kaya
  • 5,771
  • 3
  • 38
  • 52
  • The data is in your data model, not the cell. – rmaddy May 26 '15 at 05:15
  • Which value you are trying to get from Cell, seems like you are trying to log the UITableViewCell class which can never return a String. – Vizllx May 26 '15 at 05:16
  • https://stackoverflow.com/questions/28431086/getting-data-from-each-uitableview-cells-swift Just Go To This Link – kunal pal Oct 20 '17 at 18:15

1 Answers1

4
  • In your way, you get the UITableviewCell or subclass object,so what you log is right.
  • If you want to get cell value,you should check your dataSource,in MVC pattern that is your Model.
  • For example: you have a array named dataArray as Model,Then you get value dataArray[indexPath.row]

I am not sure why you set text of cell based on the indexPath.section,so ,I post example code based on the code you post

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

  Categories *category = [categories objectAtIndex:indexPath.section];
  NSString * cellText = category.CategoryName;
}

BTY,I think the right way is let text of cell based on the row

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

RewardCategoriesTableViewCell *cell = (RewardCategoriesTableViewCell*)  [tableView dequeueReusableCellWithIdentifier:@"RewardCategoriesTableViewCell"];

if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"RewardCategoriesTableViewCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}

if(indexPath.row>= [categories count]){
    return nil;
}

Categories *category = [categories objectAtIndex:indexPath.row];

NSString *identifier = [NSString stringWithFormat:@"Cell%ld", indexPath.row];

NSLog(@"%@" , identifier);

cell.lblCategory.text = category.CategoryName;

NSLog(@"cell value: %@", cell.lblCategory.text);

return cell;
}

Then in:

  -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  Categories *category = [categories objectAtIndex:indexPath.row];
  NSString * cellText = category.CategoryName;
}
Leo
  • 24,596
  • 11
  • 71
  • 92
  • This is my array in Model: NSArray* data= [[NSArray alloc]initWithObjects:category1, category2, category3, category4, category5, category6, category7, nil]; return data; Then how can i get value from here, where should i use data[indexPath.row]? – Umit Kaya May 26 '15 at 07:16
  • thank you very much, i spend my whole day in this issue. well, what do you think if i use userDefaults to store this value to transfer another view? is it good approach? – Umit Kaya May 26 '15 at 08:39