0

因此,我根据我的一个对象上的值是否 > 0 以编程方式创建 UIButton。但是,当我编辑该值并重新加载表时,它不会删除该按钮。该值绝对 > 0 而不是 nil,因为它显示在标签中。我尝试将按钮添加到每个单元格,然后设置其隐藏属性,这给了我与下面的代码相同的行为。如果我停止应用程序并重新运行应用程序,它会显示它应该如何运行。

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

    CGRect newIconRect = CGRectMake(280, 5, 33, 33);
    UIButton *warningButton = [[UIButton alloc] initWithFrame:newIconRect];
    warningButton.tag = 66;

    [cell.contentView addSubview:warningButton];
}

    UIButton *warningButton = (UIButton *)[cell.contentView viewWithTag:66];
    [warningButton setImage:[UIImage imageNamed:@"exclamation.png"] forState:UIControlStateNormal];
    [warningButton addTarget:self action:@selector(warningButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    if (ueo.daysLeft >= 0)
    {
    daysLeftLabel.text = [[NSString alloc]initWithFormat:@"%i recurringDays to go", ueo.daysLeft];

    warningButton.hidden = YES;

    }
    else
    {
    daysLeftLabel.text = [[NSString alloc]initWithFormat:@"%i recurringDays have passed", ueo.daysLeft];
    warningButton.hidden = NO;
    }
}
4

1 回答 1

0

如果您按照问题所示进行操作,则您正在单元格中创建重复的按钮。因此,您将无法删除按钮,因为您可能已经丢失了对按钮对象的引用。对于达到重新加载或表格视图滚动,上面的代码将创建新按钮并添加到单元格顶部。

您可以自定义并在该单元类UITableViewCell的方法中添加此按钮。init之后,您可以使用显示/隐藏

cell.button.hidden = YES;//or NO

一个简单但不推荐的方法是在if (cell == nil),

例如:-

if (cell == nil) {
   //create cell here
   cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

   CGRect newIconRect = CGRectMake(280, 5, 33, 33);
   UIButton *warningButton = [[UIButton alloc] initWithFrame:newIconRect];
   warningButton.tag = 100;   
   [cell.contentView addSubview:warningButton];    
}

UIButton *warningButton = (UIButton *)[cell.contenView viewWithTag:100];
NSLog(@"warningButton = %@", warningButton);
[warningButton setImage:[UIImage imageNamed:@"exclamation.png"] forState:UIControlStateNormal];
[warningButton addTarget:self action:@selector(warningButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

if (ueo.daysLeft >= 0) {
  warningButton.hidden = YES;
} else {
  warningButton.hidden = NO;
} 
于 2012-12-10T23:16:24.950 回答