ios - tableviewcell 内的 UIButtons 和 UILabels

标签 ios objective-c uitableview uibutton

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

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

-(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]; });
}

showAmount 与 showDosages 类似

最佳答案

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

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

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

关于ios - tableviewcell 内的 UIButtons 和 UILabels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18972796/

相关文章:

objective-c - 多次 md5 一个字符串

objective-c - 在 watch os 2 运行时更新 wkinterfacecontroller 的方法

ios - UITableView 在上方和下方添加空行,在将页眉和页脚设置为空 View 后仍然存在

ios - deleteRowsAtIndexPaths 不起作用但是 [self.tableView reloadData] 起作用

ios - 继承 UIButton 外观的 UITableViewCell 披露指示器

ios - 使具有关联值的枚举符合可解码?

ios - 在深层链接的情况下,以编程方式包含应用程序的反向链接到以前的应用程序

ios - 在 swift 中使用 JSON 数组

ios - 使用 AFNetworking 加载离线缓存的 JSON

Swift - 使用自定义过滤器返回 tableView 中的数组数据