user1468157
user1468157

Reputation: 55

Place a progress view in tableview cell based on cell index

I am new to iphone.I have a small question that is,I have a class CustomCell in that i have declare all elements which i have required like the below

// CustomCell.h

here in this class i am setting the getter and setter methods to TitleLabel, PercentageLabel, ProgressView, downloadButton

//  CustomCell.m

here in this class set the frame to above all,and set the image to the download button also here

screen shot of my output screen is the image which is in simulator is my output screen

// In my table view i have a download button when we click that the following method will executes

-(void)downloadButtonClicked:(id)sender
{
    NSLog(@"download button clicked");
    int index = [sender tag];
    NSLog(@"index of the cell is %d",index);
    UIButton *button = (UIButton*)sender;

    UITableViewCell *cell = (UITableViewCell *)[[button superview] superview];

    UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];

    NSLog(@"label text =%@",titleLabel.text);

    UIView *senderButton = (UIView*) sender;
    NSIndexPath *indexPath = [tableView indexPathForCell: (UITableViewCell*)[[senderButton superview]superview]]; 
    NSLog(@"indexpath is %d",indexPath.row);

    CustomCell *customCell = [[CustomCell alloc]init];
    progressView = [[UIProgressView alloc]init];
   // [customCell.contentView insertSubview:progressView atIndex:indexPath.row];
    [customCell.contentView addSubview:progressView];

    selectedBookTitle = titleLabel.text;

    NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];


    NSMutableArray *allDownloadLinks;
    biblePlayerViewController = [[BiblePlayerViewController alloc]init]; 
    allDownloadLinks = [biblePlayerViewController allDownloadLinks];
    NSLog(@"all Download Links are %@",allDownloadLinks);

    biblePlayerViewController.indexOfSelectedBookTitle = [[appDelegate getBookNames]indexOfObject:selectedBookTitle];

    Download* download = [Download downloadWithTitle:selectedBookTitle url:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.audiotreasure.com/%@.zip",[allDownloadLinks objectAtIndex:(biblePlayerViewController.indexOfSelectedBookTitle)]]]PathtoSave:documentsPath];
    [[DownloadManager sharedDownloadManager] queueDownload: download];


}

// In cellForRowAtIndexPath tableview delegate method I have written code like this

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    CustomCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
    {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }    
    NSLog(@"indexpath.row is %d",indexPath.row);
    NSLog(@"downloadmanager is %d",[[[DownloadManager sharedDownloadManager]getDownloads]count]);
  /*  if (indexPath.row<[[[DownloadManager sharedDownloadManager] getDownloads]count]) 
    {
        cell.TitleLabel.text = ((Download*)[[[DownloadManager sharedDownloadManager] getDownloads] objectAtIndex:indexPath.row]).title_;    
        cell.ProgressView.progress = (float)(((Download*)[[[DownloadManager sharedDownloadManager] getDownloads] objectAtIndex:indexPath.row]).PercentageDownloaded_ )/100.0f;
        cell.PercentageLabel.text = [NSString stringWithFormat:@"%d %%",((Download*)[[[DownloadManager sharedDownloadManager] getDownloads] objectAtIndex:indexPath.row]).PercentageDownloaded_];     
    }
    else
    {
        [tableView reloadData];
    }*/
    NSString *titleLabel = [[appDelegate getBookNames]objectAtIndex:indexPath.row];
    cell.TitleLabel.text = titleLabel;
    cell.downloadButton.tag = indexPath.row;
    NSLog(@"tag is %d",cell.downloadButton.tag);
  [cell.downloadButton addTarget:self action:@selector(downloadButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

My question is I have 66 cells in tableview in screen shot we have a bookname called Revelation.It has a 66th cell in my tableview (index is 65).Here when i click the download button in that cell,I have to show progress view in that particular cell only (65th cell).I have reload the table view for every 1 sec also because after placing the progress view we have to show the progress in that progress view in that cell.So, how can we place a progress view in that particular cell index.

Upvotes: 0

Views: 505

Answers (1)

Sorin Antohi
Sorin Antohi

Reputation: 6135

Why don't you add the progress view in the cell from the beginning? have it initially hidden and when you touch the download button you tell the cell to show it.

i see that you store the indexPath.row of the cell in the download button tag. You could use that to find out what cell was selected something like below.

NSIndexPath *cellIndexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:nowIndex];
[cell showProgressBar];

i wrote the code of top of my head..didn't tested it.

Upvotes: 1

Related Questions