UIButtons & UILabels in a tableviewcell



我在表格视图单元格内有两个UIButton和一些标签。我有 2 个部分,第一部分包含一个表视图单元格,在该单元格内有两个 UIButton,它们调用 api 为第 2 节中的单元格获取新数据。在某种 if 情况下,会创建一个标签,因为字符串很长,所以我必须为单元格创建一个新框架。

我目前遇到的问题是按钮文本也会更新新信息,有时字符串很短,有时字符串很长,所以我必须删除按钮并添加一个带有新框架的新按钮以适应新字符串。但是,如果过去的按钮字符串比新的按钮字符串短,则该按钮仍会显示在背景中。我尝试将其从超级视图中删除,并检查它是否已从超级视图中删除(一旦我删除它,请检查它是否为空(,但它仍然出现在屏幕上。同样的事情也发生在UILabels上。我还尝试使用标签(viewWithTag:123(将它们从超级视图中删除。任何人都有任何指示,想法,建议,任何可以帮助我弄清楚/解决这个问题的东西?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
        selectedBackgroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.1];
        cell.selectedBackgroundView = selectedBackgroundView;
        if(cell==nil) { NSLog(@"Cell is still nil");}
    }
    cell.textLabel.text = nil;
    cell.accessoryView = nil;
    cell.detailTextLabel.text = nil;
    cell.userInteractionEnabled = YES;
    if(indexPath.section == 0)
    {
            dosageButton                            = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            amountButton                            = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [dosageButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
            [dosageButton setContentEdgeInsets:UIEdgeInsetsMake(0, 15, 0, 0)];
            [amountButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
            [amountButton setContentEdgeInsets:UIEdgeInsetsMake(0, 15.5, 0, 0)];

            if(strengths.count == 0) { [dosageButton setTitle:@"Dosage" forState:UIControlStateNormal];}
            else
            {
                [dosageButton setTitle:selectedStrength forState:UIControlStateNormal];
                CGSize stringsize = [selectedStrength sizeWithFont:[UIFont systemFontOfSize:14]];
                [dosageButton setFrame:CGRectMake(5, 27.5, stringsize.width + 30, 30)];
            }
            if(quantity.count == 0) { [amountButton setTitle:@"Amount" forState:UIControlStateNormal];}
            else
            {
                int value = [selectedQuantity intValue];
                if (value == 1)
                {
                    NSString *amountAndFormat = [NSString stringWithFormat:@"%@ %@", selectedQuantity, selectedFormat];
                    [amountButton setTitle:amountAndFormat forState:UIControlStateNormal];
                    CGSize stringsize = [selectedStrength sizeWithFont:[UIFont systemFontOfSize:14]];
                    CGSize amountSize = [amountAndFormat sizeWithFont:[UIFont systemFontOfSize:14]];
                    if(amountSize.width+stringsize.width > 250)
                    {
                        dosageButton.frame                      = CGRectMake(5, 27.5, 130, 30);
                        amountButton.frame                      = CGRectMake(140, 27.5, 160, 30);
                    }
                    else
                    {
                        [amountButton setFrame:CGRectMake(stringsize.width + 40, 27.5, amountSize.width + (amountSize.height * 2.1), 30)];
                    }
                }
                else
                {
                    NSString *amountAndFormat = [NSString stringWithFormat:@"%@ %@", selectedQuantity, selectedFormatPlural];
                    [amountButton setTitle:amountAndFormat forState:UIControlStateNormal];
                    CGSize stringsize = [selectedStrength sizeWithFont:[UIFont systemFontOfSize:14]];
                    CGSize amountSize = [amountAndFormat sizeWithFont:[UIFont systemFontOfSize:14]];
                    if(amountSize.width + stringsize.width> 250)
                    {
                        dosageButton.frame                      = CGRectMake(5, 27.5, 130, 30);
                        amountButton.frame                      = CGRectMake(140, 27.5, 160, 30);
                    }
                    else
                    {
                        [amountButton setFrame:CGRectMake(stringsize.width + 40, 27.5, amountSize.width + (amountSize.height * 2.1), 30)];
                    }
                }
            }
            [dosageButton addTarget:self action:@selector(showDosages:) forControlEvents:UIControlEventTouchUpInside];
            [amountButton addTarget:self action:@selector(showAmount:) forControlEvents:UIControlEventTouchUpInside];
            //[cell.contentView addSubview:dosageButton];
            //[cell.contentView addSubview:amountButton];
            //adding it to view instead of cell because of userInteraction.
            //if I add it to cell.contentView and userInteractionEnabled = NO
            //then the user can't press the button
            //but if the it is enabled then they can click the cell and looks tacky!
            [self.view addSubview:dosageButton];
            [self.view addSubview:amountButton];
            cell.userInteractionEnabled = NO;
    }
    if(indexPath.section == 1)
        {
            if(localName.count == 0)
            {
                //Nothing
            }
            else
            {
            CGRect longStringFrame = CGRectMake(12, 4, 250, 120);
            //UILabel *longStringLabel = [[UILabel alloc] initWithFrame:longStringFrame];
            UILabel *longStringLabel = [[UILabel alloc] initWithFrame:longStringFrame];
            longStringLabel.backgroundColor = [UIColor clearColor];
            longStringLabel.font = [UIFont systemFontOfSize:12];
            longStringLabel.text = [NSString stringWithFormat:@"Something super duper long. Some really really long string that has a lot of information and cannot fit in one line so I have to use numberOfLines = 0 and NSLineBreakByWordWrapping. This string is really really really long.]];
            longStringLabel.textColor = [UIColor blackColor];
            longStringLabel.numberOfLines = 0;
            longStringLabel.lineBreakMode = NSLineBreakByWordWrapping;
            cell.textLabel.text = @"";
            longStringLabel.tag = 123;
            [cell.contentView addSubview:longStringLabel];
            }
        }

}

- (void) showDosages:(UIButton *)sender
{
    [dosageButton removeFromSuperview];
    [amountButton removeFromSuperview];
    [[cell.contentView viewWithTag:123] removeFromSuperview];
    NSLog(@"%@",[dosageButton superview]);
    NSLog(@"%@",[amountButton superview]);

    //Also tried it this way as well. Didn't work
    //[dosageButton performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:NO];
    //[amountButton performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:NO];
    dispatch_async(someDispatch, ^{ NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString: [NSString stringWithFormat: @"http://somewebsite.com"]]]; [self performSelectorOnMainThread:@selector(fetchData:) withObject:data waitUntilDone:YES]; });
}
显示

量类似于显示剂量

我建议您创建一个自定义单元格,放置您想要的所有标签和按钮,给出一个iboutles,以便您可以轻松管理事物。

自定义单元格教程http://mobile.tutsplus.com/tutorials/iphone/customizing-uitableview-cell/

制作单元格后,您可以根据字符串设置单元格的高度。当您的单元格大小增加或减少时,您的按钮将自动调整

这是多个单元格标识符的简单示例,希望它可能会有所帮助

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier1 = @"Cell1";
static NSString *CellIdentifier2 = @"Cell2";
if(flag == 0) {
   MainArticleTableViewCell *cell = (MainArticleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
   if (cell == nil) {
       cell = [[[MainArticleTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier1] autorelease];
   }
   cell.textLabel.text= @"Cell1";
   return cell;
}
else {
   NewsTableViewCell *cell = (NewsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
   if (cell == nil) {
       cell = [[[NewsTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier2 orientation:currentOrientation] autorelease];
   }
   cell.textLabel.text= @"Cell1";
   return cell;
}
return nil;
}

最新更新