开发者

update UItableviewcell after button tapped for cell

开发者 https://www.devze.com 2023-03-12 05:45 出处:网络
Hello guys i have cells in tableview, but there are two different UIButtons to be assigned to cell. Depend on if the filename those cell indication is there in the sandbox.

Hello guys i have cells in tableview, but there are two different UIButtons to be assigned to cell. Depend on if the filename those cell indication is there in the sandbox.

i am doing this by following function while creating cell.

NSLog(@"%@", filepath);

        if ([[NSFileManager defaultManager] fileExistsAtPath:filepath]) 
        {
            [button setTitle:@"Submit" forState:UIControlStateNormal];
            [button addTarget:self action:@selector(SubmitData:) forControlEvents:UIControlEventTouchUpInside];
        }

        else
        {
            [button setTitle:@"Fetch" forState:UIControlStateNormal];
            [button addTarget:self action:@selector(FetchData:) forControlEvents:UIControlEventTouchUpInside];

        }

but the thing is that when i press Fetch button and when FetchData function is called, i fetch the data and save it in the sandbox. So i want to update the button to Submit as now the file is their.

So do i have to put something in FetchData function to update the cell button to Submit,

I tried using [self.tableview reloaddata];

but won't update the cell.

FULL CODE

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *FirstLevelCell= @"FirstLevelCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstLevelCell];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  reuseIdentifier:FirstLevelCell] autorelease];
        NSDictionary * assignment = [assignments objectAtIndex:[indexPath row]];
        cell.textLabel.text= [NSString stringWithFormat:@"Riding Number is %@", [assignment objectForKey:@"Riding_Number"]];
        cell.detailTextLabel.text= [NSString stringWithFormat:@"Poll Number is %@",[assignment objectForKey:@"Poll"]];
        UIImage *buttonUpImage = [UIImage imageNamed:开发者_StackOverflow@"button_up.png"];
        UIImage *buttonDownImage = [UIImage imageNamed:@"button_down.png"];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0.0, 0.0, (buttonUpImage.size.width)*1.20,
                                  buttonUpImage.size.height);
        [button setBackgroundImage:buttonUpImage forState:UIControlStateNormal];
        [button setBackgroundImage:buttonDownImage forState:UIControlStateHighlighted];
          button.tag = [indexPath row];  
        NSString * filepath = [self dataFilePathwithFilename:[NSString stringWithFormat:@"%@_%@.plist",[assignment objectForKey:@"Riding_Number"],[assignment objectForKey:@"Poll"]]];


        if ([[NSFileManager defaultManager] fileExistsAtPath:filepath]) 
        {
            [button setTitle:@"Submit" forState:UIControlStateNormal];
            [button addTarget:self action:@selector(SubmitData:) forControlEvents:UIControlEventTouchUpInside];
        }

        else
        {
            [button setTitle:@"Fetch" forState:UIControlStateNormal];
            [button addTarget:self action:@selector(FetchData:) forControlEvents:UIControlEventTouchUpInside];

        }

        cell.accessoryView = button;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }

    // Configure the cell...

    return cell;

}


set the closing } for if (cell == nil) right after cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:FirstLevelCell] autorelease];
it says in your comment where you should configure the cell!. the if-not-nil statement is for creation only!! after this block you have to configure the cell (set texts, buttons, images, accessoryView, ...). Just remember the cell could be in ANY state. When a cell leaves the visible area (topmost cell when you scroll down), it will be put in a pool where you grab it out again with dequeueReusableCellWithIdentifier and configure it to match the required index path (bottom cell that slides into the screen when you scroll down).

0

精彩评论

暂无评论...
验证码 换一张
取 消