Blios
Blios

Reputation: 729

Change a label on cells of UITableView?

I've added 3 labels and a button on cell of UITableView like this image screen shot

when i click on checkbox i change the label 'Due in 0 days' in some other stuff. it is ok with this code

[statusText setTextColor:[UIColor colorWithRed:0.0/255.0 green:51.0/255.0 blue:102.0/255.0 alpha:1.0]];
[statusText  setFont:[UIFont boldSystemFontOfSize:16.0]];
statusText.tag = 100;
[statusText setBackgroundColor:[UIColor clearColor]];
[checkBoxBtn setBackgroundImage:[UIImage imageNamed:@"check_normal.png"] forState:UIControlStateNormal];
[checkBoxBtn addTarget:self action:@selector(showAlert:) forControlEvents:UIControlEventTouchUpInside];
checkBoxBtn.tag = indexPath.row;
[cell.contentView addSubview:checkBoxBtn];

this is my checkBoxAction and showAlert methods

-(void) checkBoxAction: (UIButton *)sender 
{

    NSInteger i =sender.tag + 1;
    float perc = (i*100/18.0);
    //NSLog(@"%.2f",perc);
    NSString* percentageStr = [[NSString alloc] initWithFormat:@"%3.2f%%(%d out of 18)",perc, i];
    barTitle.text = percentageStr;
    barFGImage.hidden = NO;
    if (perc == 100.00) {
             [barFGImage setFrame:CGRectMake(barFGImage.frame.origin.x, barFGImage.frame.origin.y, 276.0, barFGImage.frame.size.height)];
        }
        else
            [barFGImage setFrame:CGRectMake(barFGImage.frame.origin.x, barFGImage.frame.origin.y, 280*perc/100, barFGImage.frame.size.height)];

        [sender setBackgroundImage:[UIImage imageNamed:@"check_select.png"] forState:UIControlStateNormal];

    sender.userInteractionEnabled = NO;

    NSString *currentDate = [formatter stringFromDate:[NSDate date]];
    NSString* statusStr = [[NSString alloc] initWithFormat:@"Completed on %@",currentDate];
    UILabel *tempLabel = (UILabel *)[[button superview] viewWithTag:100];
    tempLabel.textColor = [UIColor colorWithRed:243.0/255.0 green:134.0/255.0 blue:48.0/255.0 alpha:1.0];
    tempLabel.text = statusStr;

}

- (void) showAlert:(id)sender
{
    button = (UIButton *)sender;
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"Do you really want to complete this training?" delegate:self cancelButtonTitle:@"Yes, I've completed it" otherButtonTitles: @"No", nil];
    [alert show];
}

Now i want that if i press checkBox on row (say)3rd then 'Due in 0 days' label of row 3rd, 4th and so on should changed.Any suggestion or sample code will be appreciated. Thanks.

Upvotes: 0

Views: 248

Answers (1)

JP Hribovsek
JP Hribovsek

Reputation: 6707

I am afraid my suggestion is not going to be a simple suggestion or code sample, but rather I would invite you to spend some time reading about MVC (Model View Controller).

From what I can tell from your code, your data seems to be 'stored' in your view objects. This is a very bad idea in general, this is a terrible idea with table views, as cells are being reused, and your data would just disappear as you scroll through large tables.

What you need to do is define model classes to hold your data, the controller objects will then access those model objects and display their data in the various views they control.

Once you implement this kind of pattern, checking something in one cell will update your model objects. Then all you will need to do is to make a simple [tableView reloadData]; call to update ALL your cells at once.

Upvotes: 1

Related Questions