用户交互启用更改其他部分的文本颜色和可点击性

时间:2013-09-16 21:55:00

标签: ios objective-c uitableview tableviewcell

我在表格视图中有3个部分,仅使用中间部分第2部分来显示各种单元格。第1节和第3节只显示一个单元格,因为我想在它们上面显示按钮和文本,所以我将它们变为不可点击。我做了它们,它工作正常,直到我做了第1和第3部分userInteractionEnabled = NO。

代码:我知道我可以使这个面向对象,它确实如此,但是一旦出现这个问题,我试着改变它,但它仍然是相同的。

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

    if(indexPath.section == 0)
    {
        cell.textLabel.text = nil;
        cell.accessoryView = nil;
        cell.detailTextLabel.text = nil;
        dosageButton                            = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        amountButton                            = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [dosageButton addTarget:self action:@selector(showDosages:) forControlEvents:UIControlEventTouchUpInside];
        [amountButton addTarget:self action:@selector(showAmount) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:dosageButton];
        [self.view addSubview:amountButton];
        cell.userInteractionEnabled = NO;
        return cell;
    }

    else if (indexPath.section == 1)
    {
        if (self.nameMutable.count != 0 )
        {
            cell.textLabel.text = [self.nameMutable objectAtIndex:indexPath.row];
            cell.detailTextLabel.text = [NSString stringWithFormat:@"$%@",[self.priceMutable objectAtIndex:indexPath.row]];
            cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"chevron"]];
            return cell;
        }
        else
        {
            //Empty for now. Waiting for data fetching to finish
            return cell;
        }
    }
    else if (indexPath.section == 2)
    {
        cell.userInteractionEnabled = NO;
        cell.accessoryView = nil;
        cell.detailTextLabel.text = nil;
        return cell;
    }
    else
    {
        NSLog(@"Something went wrong");
        return cell;
    }

}

出于某种原因,我的表格在第1部分中查看单元格,它应该是可点击的,颜色变为深灰色,不再可点击。它通常是单元格3和单元格10.此外,当我向下滚动并且第0部分不再可见时,然后我向上滚动并且第0部分可见,一些单元格变得不可点击并且文本的颜色发生变化。 / p>

另外,如何在第1部分内创建一个特定单元格,因为文本太长而无法显示,并且它开始显示“...”或覆盖detailTextLabel。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您必须记住,这些单元格正在重复使用或“回收”,因此如果您为userInteractionEnabled=NO语句设置if,则需要在userInteractionEnabled=YES中将其设置为else {1}}语句,或在所有语句之前将其设置为YES。您还需要确保将某些索引路径所特有的任何其他子视图(按钮等)添加到新创建的单元格中,您可以将这段代码粘贴到if(cell==nil)语句中。像这样:

-(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(indexPath.section == 0) {
            dosageButton                            = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            amountButton                            = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [dosageButton addTarget:self action:@selector(showDosages:) forControlEvents:UIControlEventTouchUpInside];
            [amountButton addTarget:self action:@selector(showAmount) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:dosageButton];
            [self.view addSubview:amountButton];
        }

        if(cell==nil) { NSLog(@"Cell is nil");}
    }

    cell.userInteractionEnabled = YES;
    cell.accessoryView = nil;
    cell.textLabel.text = nil;
    cell.detailTextLabel.text = nil;

    if(indexPath.section == 0)
    {
        cell.userInteractionEnabled = NO;
    }

    else if (indexPath.section == 1)
    {
        if (self.nameMutable.count != 0 )
        {
            cell.textLabel.text = [self.nameMutable objectAtIndex:indexPath.row];
            cell.detailTextLabel.text = [NSString stringWithFormat:@"$%@",[self.priceMutable objectAtIndex:indexPath.row]];
            cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"chevron"]];
        }
        else
        {
            //Empty for now. Waiting for data fetching to finish
        }
    }
    else if (indexPath.section == 2)
    {
        cell.userInteractionEnabled = NO;
    }
    else
    {
        NSLog(@"Something went wrong");
    }
    return cell;
}

如果您想更改某些索引路径的高度(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath Docs委托方法。