Ios 香港邮政;UITableViewCell中的标签
在表视图单元格中有两个UIButton和一些标签。我有两个部分,第一部分包含一个表视图单元格,在该单元格内有两个UIButton,它们调用api为第2部分中的单元格获取新数据。在特定的if情况下,会创建一个标签,因为字符串很长,所以我必须为单元格创建一个新的框架 我目前遇到的问题是,按钮文本也会更新为新信息,有时字符串很短,有时字符串很长,因此我必须删除按钮并添加一个带有新框架的新按钮以适应新字符串。但是,如果过去的按钮字符串比新按钮字符串短,则按钮仍会显示在背景中。我尝试从superview中删除它,并检查它是否已从superview中删除(删除后检查它是否为空),确实是,但它仍然显示在屏幕上。同样的事情也发生在UILabel上。我还尝试使用标记(viewWithTag:123)将它们从超级视图中删除。有没有人能给我一些建议、想法、建议,或者任何能帮助我解决这个问题的东西Ios 香港邮政;UITableViewCell中的标签,ios,objective-c,uitableview,uibutton,Ios,Objective C,Uitableview,Uibutton,在表视图单元格中有两个UIButton和一些标签。我有两个部分,第一部分包含一个表视图单元格,在该单元格内有两个UIButton,它们调用api为第2部分中的单元格获取新数据。在特定的if情况下,会创建一个标签,因为字符串很长,所以我必须为单元格创建一个新的框架 我目前遇到的问题是,按钮文本也会更新为新信息,有时字符串很短,有时字符串很长,因此我必须删除按钮并添加一个带有新框架的新按钮以适应新字符串。但是,如果过去的按钮字符串比新按钮字符串短,则按钮仍会显示在背景中。我尝试从superview中
-(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类似我建议您创建一个自定义单元格,放置您想要的所有标签和按钮,并提供一个iButles,以便您可以轻松管理内容 自定义单元教程
制作单元后,可以根据字符串设置单元的高度。当单元格大小增加或减少时,按钮将自动调整。下面是一个简单的多单元格标识符示例,希望对您有所帮助
- (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;
}
请添加您的代码以供参考…请将您的代码与您的tableview@NANNAV编辑OP并添加code@Unheilig问题是我再次使用相同的按钮。我编辑了文章并添加了代码。@JayGajjar编辑了文章并添加了代码问题是我使用单元格来显示cell.textLabel.text以及label.text。这不是会在每个单元格上创建按钮吗?我以前使用过自定义单元格,但我从未在其中放置过按钮,因此我不知道每个单元格是否会创建一个按钮,因为
if(cell==nil){//create custom cells here}
@Chris-您已经在所有单元格中放置了按钮。当你回收一个单元格时,以前在其中的任何按钮都将仍然存在。如果您只想在某些单元格中使用按钮,请使用单元格标识符功能将它们分为两类。@HotLicks I am?为什么呢我以为我只是将它添加到第一个部分,即section==1,并在其中创建它。我如何将它们分为两类?当单元格从屏幕的末尾滚下时,它们进入一个池中,基于“reuseIdentifier”值。您只指定了一个标识符,因此只有一个池。谢谢!我将其与自定义单元格一起使用,并在单元格内创建按钮,然后返回它们。