TableView单元格模糊

时间:2011-09-18 07:31:10

标签: iphone objective-c

我在查看tableview中的单元格时遇到了一个有趣的问题:  首先加载tableview时,单元格看起来有点模糊,奇怪的是,当显示然后隐藏键盘时,它们将被固定,直到再次滑动表格(使新单元格出列)。 Before

After

通常,该tableview中有3种类型的单元格(CustomCellWithButton,AddSlidesCells和CustomCell)。模糊可以在所有这些中找到,但在CustomCell中可以清楚地看到它。它让我变得模糊:S请帮忙

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 static NSString *CellIdentifier;
 if((indexPath.section == 4 && (indexPath.row == 1)))
 {
    CellIdentifier = @"CustomCellWithButton";
    CustomCellWithButton *cell = (CustomCellWithButton*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCellWithButton" owner:nil options:nil];

        for(id currentObject in topLevelObjects){
            if([currentObject isKindOfClass:[UITableViewCell class]]){
                cell = (CustomCellWithButton *)currentObject;
                break;
            }
        }
    }
    [cell.button addTarget:self action:@selector(nextButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [cell setBackgroundColor:[UIColor darkGrayColor]];

    return cell;
}
else
{
    if((indexPath.section == 3 && (indexPath.row == 0))){
        CellIdentifier = @"AddSlidesCells";

        AddSlidesCells *cell = (AddSlidesCells*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"AddSlidesCells" owner:nil options:nil];

            for(id currentObject in topLevelObjects){
                if([currentObject isKindOfClass:[UITableViewCell class]]){
                    cell = (AddSlidesCells *)currentObject;
                    break;
                }
            }
        }

        [cell.switchButton setOn:[self getswitchButtonsStatesByIndexPath:indexPath] withIndexpath:indexPath];
        [cell.switchButton addTarget:self action:@selector(switchChanged:forEvent:) 
                    forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)];
        [cell.button addTarget:self action:@selector(addSlicePressed:) forControlEvents:UIControlEventTouchDown];

        NSLog(@" ---- here add to dismiss keyboared");

        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [cell setBackgroundColor:[UIColor darkGrayColor]];

        if (alignedLeft) {
            [cell hardAllignToLeft];

        }else
            [cell hardAllignToRight];

        return cell;
    }

    else
    {
        CellIdentifier = @"CustomCell";
        BOOL dequed;
        CustomCell *cell = (CustomCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        NSString* placeHolder = [self getplaceHoldersByIndexPath:indexPath];

        if (cell == nil) {
            dequed = NO;
            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];

            for(id currentObject in topLevelObjects){
                if([currentObject isKindOfClass:[UITableViewCell class]]){
                    cell = (CustomCell *)currentObject;
                    [cell setFrame:CGRectMake(0, 0, 320, 50)];

                    break;
                }
            }
        }else
            dequed = YES;

        cell.alignedLeft = alignedLeft;

        [cell.textField setPlaceholder:placeHolder];
        [cell.textField setText:[self getTextfieldByIndexPath:indexPath] andIndexPath:indexPath];
        [cell.switchButton setOn:[self getswitchButtonsStatesByIndexPath:indexPath] withIndexpath:indexPath];
        [cell.switchButton addTarget:self action:@selector(switchChanged:forEvent:)
                    forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)];
        [cell.textField  addTarget:self action:@selector(UIControlEventEditingChanged:) 
                  forControlEvents:UIControlEventEditingChanged];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.textField.delegate = self;

        if( [placeHolder isEqualToString:[OperationsOnCoreData PlistCommonStrings:@"Telephone"]] ||
           [placeHolder isEqualToString:[OperationsOnCoreData PlistCommonStrings:@"Email"]]){

            cell.plusSignButton.hidden = NO;
            cell.minusSignButton.hidden = YES;
            [cell.plusSignButton addTarget:self action:@selector(plusSignButtonPressed:)
                          forControlEvents:UIControlEventTouchUpInside];
        }
        else
            if(( !([placeHolder rangeOfString:[NSString stringWithFormat:@"%@ ",[OperationsOnCoreData PlistCommonStrings:@"Telephone"]]].location == NSNotFound)||
                !([placeHolder rangeOfString:[NSString stringWithFormat:@"%@ ",[OperationsOnCoreData PlistCommonStrings:@"Email"]]].location == NSNotFound))){

                cell.minusSignButton.hidden = NO;
                cell.plusSignButton.hidden = YES;
                [cell.minusSignButton addTarget:self action:@selector(minusSignButtonPressed:) 
                               forControlEvents:UIControlEventTouchUpInside];
            }
            else{
                cell.minusSignButton.hidden = YES;
                cell.plusSignButton.hidden = YES;                            
            }


        if(cell.switchButton.on){
            [cell.textField setEnabled:YES];
            cell.textField.alpha = 1; 
        }else{
            [cell.textField setEnabled:NO];
            cell.textField.alpha = 0.5;
            cell.plusSignButton.enabled = NO;
            cell.minusSignButton.enabled = NO;
        }

        [cell setBackgroundColor:[UIColor darkGrayColor]];  

        if (alignedLeft) {
                [cell hardAllignToLeft];

        }else
                [cell hardAllignToRight];
        return cell;
    }
}

}

2 个答案:

答案 0 :(得分:1)

我不确定您是否可以将您的小区标识符定义为static,然后在每次调用时更改它。您应该定义三个静态字符串变量(每种类型一个),然后在每种情况下使用适当的变量。我想知道这是否会阻止您的单元格正确出列,或者是否使用错误的标识符添加到表中。

通常情况下,模糊问题是在错误的位置向单元格添加子视图时(即,当您第一次初始化单元格时),但这似乎不是这种情况。但是,您正在执行许多操作(设置操作等),这些操作似乎每行都没有更改,因此在您的cell == nil语句中更好地完成这些操作。

答案 1 :(得分:1)

模糊细胞看起来很模糊,因为元素比非模糊细胞高一个像素。因此,模糊的外观是由调整开关按钮和加号图标的大小引起的。

要修复它,您需要调查单元格的布局,特别是这些元素的大小。他们可能应该总是采取自然的大小。粘贴的代码不包含相关部分。