Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UITableViewCell中的UILabel重叠_Iphone_Ios_Cocoa Touch_Uitableview_Uilabel - Fatal编程技术网

Iphone UITableViewCell中的UILabel重叠

Iphone UITableViewCell中的UILabel重叠,iphone,ios,cocoa-touch,uitableview,uilabel,Iphone,Ios,Cocoa Touch,Uitableview,Uilabel,我有2个UILabels,1个标题1个副标题, 选择segmentedControl时要更改的 它可以工作,但是当选择不同的段时,我会得到相同的UILabel自身重叠 我想我需要创建一个操作,在superview将标签重新显示到单元格上之前将其从superview中删除?只是想知道怎么做 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

我有2个UILabels,1个标题1个副标题, 选择segmentedControl时要更改的

它可以工作,但是当选择不同的段时,我会得到相同的UILabel自身重叠

我想我需要创建一个操作,在superview将标签重新显示到单元格上之前将其从superview中删除?只是想知道怎么做

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    EventUpcoming *aEventUpcoming = [euEvent objectAtIndex:indexPath.section];
    EventWeekly *aEventWeekly = [ewEvent objectAtIndex:indexPath.section];

    UILabel *cellTitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, 290, 20)];
    UILabel *cellSubtitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 290, 20)];

    NSString *titleString = [[NSString alloc] init];
    NSString *subtitleString = [[NSString alloc] init];

    if (segmentedControl.selectedSegmentIndex == 0) 
    {
        Event *aEvent = [aEventUpcoming.event objectAtIndex:indexPath.row];
        titleString = aEvent.name;
        subtitleString = aEvent.subtitle;
    } 
    else 
    {
        Event *aEvent = [aEventWeekly.event objectAtIndex:indexPath.row];
        titleString = aEvent.name;
        subtitleString = aEvent.subtitle;
    }

    NSString *titleStringUC = [titleString uppercaseString];
    NSString *subtitleStringLC = [subtitleString lowercaseString];

    cellTitle.text = titleStringUC;
    cellTitle.font = [UIFont boldSystemFontOfSize:11];
    cellTitle.textColor = [UIColor colorWithRed:142/255.0f green:142/255.0f blue:142/255.0f alpha:1];
    cellTitle.shadowColor = [UIColor whiteColor];
    cellTitle.shadowOffset = CGSizeMake(1, 1);
    cellTitle.backgroundColor = [UIColor clearColor];

    cellSubtitle.text = subtitleStringLC;
    cellSubtitle.font = [UIFont boldSystemFontOfSize:11];
    cellSubtitle.textColor = [UIColor colorWithRed:177/255.0f green:177/255.0f blue:177/255.0f alpha:1];
    cellSubtitle.shadowColor = [UIColor whiteColor];
    cellSubtitle.shadowOffset = CGSizeMake(1, 1);
    cellSubtitle.backgroundColor = [UIColor clearColor];


    tableViewCellSeparator = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TableCellSeparator.png"]];
    tableViewCellSeparator.frame = CGRectMake(0, cell.bounds.size.height - 2, 320, 2);

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    [cell.contentView addSubview:cellTitle];
    [cell.contentView addSubview:cellSubtitle];
    [cell.contentView addSubview:tableViewCellSeparator];






    return cell;
}


更新:

这两个都是非常有效的答案,tyvm

您没有正确地重用您的计算单元,因此您没有从重用中获得性能优势,并使代码更加复杂。你也没有使用苹果公司提供给你的现成产品

首先,您应该在
单元==nil
块中创建并添加所有子视图。这是创建可重用单元的地方。在接下来的例行程序中,您只需重新配置单元。这要快得多

其次,
UITableViewCell
已经内置了两个标签。您不需要创建它们。它们被称为
textlab
detailtextlab
。它们是普通的标签;你可以移动它们,让它们看起来像你想要的任何东西。在
单元格==nil
块中执行此操作

然后您只需要在
cell==nil
块外调用
cell.textlab.text=…
cell.detailtextlab.text=…
,就可以开始了


如果您需要的标签多于两个,那么我将对
UITableViewCell
进行子类化,并在其上创建新属性,以便您可以轻松地重新配置单元格。

我希望它现在可以工作。请尝试此

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];


    EventUpcoming *aEventUpcoming = [euEvent objectAtIndex:indexPath.section];
    EventWeekly *aEventWeekly = [ewEvent objectAtIndex:indexPath.section];

    UILabel *cellTitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, 290, 20)];
    UILabel *cellSubtitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 290, 20)];

    NSString *titleString = [[NSString alloc] init];
    NSString *subtitleString = [[NSString alloc] init];

    NSString *titleStringUC = [titleString uppercaseString];
    NSString *subtitleStringLC = [subtitleString lowercaseString];

    cellTitle.text = titleStringUC;
    cellTitle.font = [UIFont boldSystemFontOfSize:11];
    cellTitle.textColor = [UIColor colorWithRed:142/255.0f green:142/255.0f blue:142/255.0f alpha:1];
    cellTitle.shadowColor = [UIColor whiteColor];
    cellTitle.shadowOffset = CGSizeMake(1, 1);
    cellTitle.backgroundColor = [UIColor clearColor];
    cellTitle.tag = 10;

    cellSubtitle.text = subtitleStringLC;
    cellSubtitle.font = [UIFont boldSystemFontOfSize:11];
    cellSubtitle.textColor = [UIColor colorWithRed:177/255.0f green:177/255.0f blue:177/255.0f alpha:1];
    cellSubtitle.shadowColor = [UIColor whiteColor];
    cellSubtitle.shadowOffset = CGSizeMake(1, 1);
    cellSubtitle.backgroundColor = [UIColor clearColor];
    cellSubtitle.tag = 11;


    tableViewCellSeparator = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TableCellSeparator.png"]];
    tableViewCellSeparator.frame = CGRectMake(0, cell.bounds.size.height - 2, 320, 2);

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


    [cell.contentView addSubview:cellTitle];
    [cell.contentView addSubview:cellSubtitle];
    [cell.contentView addSubview:tableViewCellSeparator];
}

cellTitle = (UILabel *)[cell.contentView viewWithTag:10];
 cellSubtitle = (UILabel *)[cell.contentView viewWithTag:11];

if (segmentedControl.selectedSegmentIndex == 0) 
{
    Event *aEvent = [aEventUpcoming.event objectAtIndex:indexPath.row];
    titleString = aEvent.name;
    subtitleString = aEvent.subtitle;
} 
else 
{
    Event *aEvent = [aEventWeekly.event objectAtIndex:indexPath.row];
    titleString = aEvent.name;
    subtitleString = aEvent.subtitle;
}

return cell;
}