按钮点击单元格后更新UItableviewcell

时间:2011-06-10 15:43:30

标签: uitableview

大家好我在tableview中有单元格,但是有两个不同的UIButtons要分配给单元格。取决于文件名是否在沙盒中存在那些单元格指示。

我在创建单元格时通过跟随功能来执行此操作。

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];

        }

但问题是,当我按下Fetch按钮并调用FetchData函数时,我获取数据并将其保存在沙箱中。所以我想更新按钮提交,因为现在文件是他们的。

所以我必须在FetchData函数中添加一些内容来将单元格按钮更新为Submit,

我尝试使用[self.tableview reloaddata];

但不会更新单元格。

完整代码

- (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:@"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;

}

1 个答案:

答案 0 :(得分:0)

}之后立即为if (cell == nil)设置结束cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:FirstLevelCell] autorelease];
它在你的评论中说你应该配置单元格! if-not-nil语句仅适用于创建!在此块之后,您必须配置单元格(设置文本,按钮,图像,accessoryView,...)。请记住,细胞可能处于任何状态。当一个单元格离开可见区域(向下滚动时最顶层的单元格),它将被放入一个池中,您可以使用dequeueReusableCellWithIdentifier再次将其取出并配置它以匹配所需的索引路径(当您使用时滑入屏幕的底部单元格向下滚动。)