-1

I have a problem: I want to get indexPath of recent cell added on UITableView. Here is my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //[_tableView clearsContextBeforeDrawing];
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];

    }


    cell.tag = indexPath.row;

    CGRect frame1 = CGRectMake(10, 25, 80, 10);
    UILabel *lbl2= [[UILabel alloc]initWithFrame:frame1];
    [lbl2 setFont:[UIFont fontWithName:@"Helvetica" size:8.5]];
    [lbl2 setTextColor:[UIColor blackColor]];
    [lbl2 setTextAlignment:UITextAlignmentLeft];
    lbl2.text = [FileDateArray objectAtIndex:indexPath.row];
    [cell addSubview:lbl2];
    [lbl2 release];

    deleteIndexPath = indexPath.row;
    NSLog(@"Delete index:%@",deleteIndexPath);

    return cell;

}

On my tableview only one row added a time. When I print on console, deleteIndexPath is null. Do you have any suggestion?

Kapil Choubisa
  • 5,152
  • 9
  • 65
  • 100
NGOT
  • 149
  • 5
  • 12
  • 1
    What is your deleteIndexPath type? is it Int? But why NSLog it as it is object? – brianLikeApple Jul 05 '13 at 09:35
  • NSLog(@"%ld",(long)indexPath.row); log it and check it. – Jitendra Jul 05 '13 at 09:42
  • @brianLikeApple : it is NSIndexPath type – NGOT Jul 05 '13 at 09:43
  • how do you add new row? – Niru Mukund Shah Jul 05 '13 at 09:54
  • @NGOT if it is a NSIndexPath type than don't you think you need to use it like `deleteIndexPath = indexpath;` – Kapil Choubisa Jul 05 '13 at 10:07
  • also the code you given is for `cellForRowAtIndexPath` please provide us the code where you adding row in tableview. How many sections you are having and where you are adding the row i.e at bottom (last row) or anywhere based on your condition? – Kapil Choubisa Jul 05 '13 at 10:09
  • - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [FileNameArray count]; } This is my code add row – NGOT Jul 05 '13 at 10:13

1 Answers1

0

I am not clear what you want. I can assume for the following three possibilities. Please check if any one you want. If not than please let me know which indexpath you want and where you want to access it.

If you want to get last indexpath of your tableView than you can try as follow:

NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow:[FileNameArray count] - 1 inSection:0];

this will return you the last indexpath possible for your tableview.

If you want to get indexpaths for visible cell than you can use

NSArray *visibleCellIndexPaths = [yourTableViewObject indexPathsForVisibleRows];

If you want to get indexpath for last visible cell than you can use

[[yourTableViewObject indexPathsForVisibleRows] lastObject];

UPDATE

You want to get indexpath of first row of tableview than you can use the first solution just need to pass 0 in row.

NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];

because index starts from 0 for both rows and section. As you have only one section you will have 0 index for section and you want to get first row so you will have 0 index for the first row.

Hope this helps you.

Kapil Choubisa
  • 5,152
  • 9
  • 65
  • 100
  • Thanks Kapil Choubisa, I want to get indexpath of first row in tableview. DO you show me? Thanks a lot – NGOT Jul 05 '13 at 10:56
  • @NGOT please check the updated answer and if it fulfil your requirement please mark it as accepted answer. – Kapil Choubisa Jul 05 '13 at 11:02
  • I wrote a updatearray function and it call loop after 2s, so i use your code get indexpath of first row, INdexPath changed after call updatearray function. So it not work. – NGOT Jul 05 '13 at 11:09
  • No matter.. your indexpath for the first row will be always 0,0. But yes your cell for that indexpath will be changed. In that case you have to store the cell instead of indexpath – Kapil Choubisa Jul 05 '13 at 11:13
  • I think so, do you show me how to store cell instead of indexpath? Thanks much – NGOT Jul 08 '13 at 01:58
  • @NGOT is there any specific event where you want to store the cell or indexpath. i.e you want to store `selected` cell or where `name == ios`. Some condition must be there. You can't do this in `tableView:cellForRowAtIndexPath:` without some condition because it will call multiple time. That is what I am asking which row, what condition is there where you want to store cell. Please explain. – Kapil Choubisa Jul 08 '13 at 04:44