DeviPhone26
DeviPhone26

Reputation: 615

button.tag property is not working in UItableView cell

I am adding download button in cell's accessory view ..my code is

button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = [UIImage imageNamed:@"download.png"];
[button setImage:image forState:UIControlStateNormal];
 //[button setTitle:@"Download" forState:UIControlStateNormal];
[button setFrame: CGRectMake( 110.0f, 3.0f, 80.0f, 30.0f)];
[button addTarget:self action:@selector(someAction) forControlEvents:UIControlEventTouchUpInside];
button.tag = indexPath.row;
cell.accessoryView = button;

And I want to pass file name from cell 's index path but this is not working It only takes first row...

- (void)someAction {

    [self.activityIndicator startAnimating];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *file = [NSString stringWithFormat:@"/%@",[metaArray objectAtIndex:button.tag]]; 
    NSLog(@"%@", button.tag);   
    NSString *path = [NSString stringWithFormat:@"%@/Downloaded Data/%@",[paths objectAtIndex:0],file];

//  NSString *loadFileName = [NSString stringWithFormat:@"/%@/%@",Name,file];
    [self.restClient loadFile:file intoPath:path];
    NSLog(@"Downloaded:%@",file);
}

This crashing because of button.tag = indexpath.row and if I don't pass button.tag = indexpath.row it is passing only first row please help

Upvotes: 1

Views: 747

Answers (2)

yasirmturk
yasirmturk

Reputation: 1954

rows of UITableView are reusable, dont set the tag with row index instead us this method

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
    id rowObject = [myArray objectAtIndex:indexPath.row];
}

Upvotes: 1

EmptyStack
EmptyStack

Reputation: 51374

tag is an integer property. You are trying to print it as object in NSLog. You have to print it like this, NSLog(@"%d", button.tag);

Actually you have to assign individual download buttons to each cell.

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = [UIImage imageNamed:@"download.png"];
[button setImage:image forState:UIControlStateNormal];
//[button setTitle:@"Download" forState:UIControlStateNormal];
[button setFrame: CGRectMake( 110.0f, 3.0f, 80.0f, 30.0f)];
[button addTarget:self action:@selector(someAction:) forControlEvents:UIControlEventTouchUpInside];
button.tag = indexPath.row;
cell.accessoryView = button;

And define your someAction method like this,

- (void)someAction:(id)sender {

    UIButton *button = (UIButton *)sender;
    NSLog(@"%d", button.tag); 
    [self.activityIndicator startAnimating];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *file = [NSString stringWithFormat:@"/%@",[metaArray objectAtIndex:button.tag]];   
    NSString *path = [NSString stringWithFormat:@"%@/Downloaded Data/%@",[paths objectAtIndex:0], file];
   //NSString *loadFileName = [NSString stringWithFormat:@"/%@/%@",Name,file];
    [self.restClient loadFile:file intoPath:path];
    NSLog(@"Downloaded:%@", file);
}

Upvotes: 2

Related Questions