簡體   English   中英

表格視圖中重疊的單元格

[英]Cells overlapping in table view

我正在嘗試實現具有單元格和文本字段的UITableView,但是單元格保持重疊並且文本字段正在從一個部分跳轉到另一部分。

有人可以檢查我的代碼是否遇到任何錯誤?

listForItemsinfo =[[NSArray alloc]initWithObjects:@"Name",@"Price", nil];
listForBrandYear  =[[NSArray alloc] initWithObjects:@"Year",@"Alfa",@"aston",@"audi",@"bentley",@"BMW",@"Cadillac",@"Chevrolet",@"Ferrari",@"Ford",@"Jaguar",@"Jeep",@"Kia",@"Landrover",@"Mazda",@"Mercedes",@"Mitsubishi",@"Nissan",@"Porshe",@"Subaru", nil];



listForkeywords  =[[NSArray alloc] initWithObjects:@"KeyWord1",@"Keyword2", nil];
listForcarType  =[[NSArray alloc] initWithObjects:@"Sedan",@"Convertible",@"Coupe",@"Heatchback", nil];
listForItemsCategory =[[NSArray alloc] initWithObjects:@"cat1",@"cat2",@"cat3",@"cat4",@"cat5",@"cat6", nil];

sections = [[NSArray alloc]initWithObjects:@"Item's info",@"Brand, Year",@"Keywords",@"Car Type",@"Category", nil];
listOfsections =[[NSArray alloc]initWithObjects:listForItemsinfo,listForBrandYear,listForkeywords,listForcarType,listForItemsCategory, nil];



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


    if (indexPath.section == 0) {

            // Add a UITextField
        UITextField *textField = [[UITextField alloc] init];
        // Set a unique tag on each text field
        textField.tag = 1+ indexPath.row;
        // Add general UITextAttributes if necessary
        textField.enablesReturnKeyAutomatically = YES;
        textField.autocorrectionType = UITextAutocorrectionTypeNo;
        textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        [cell.contentView addSubview:textField];


    }else if (indexPath.section == 1 ){
        if (indexPath.row==0) {

        UITextField *textField = [[UITextField alloc] init];
        // Set a unique tag on each text field
        textField.tag = 2+ indexPath.row;
        // Add general UITextAttributes if necessary
        textField.enablesReturnKeyAutomatically = YES;
        textField.autocorrectionType = UITextAutocorrectionTypeNo;
        textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        [cell.contentView addSubview:textField];
        }
    }else if (indexPath.section == 2 ){
        if (indexPath.row==0 | indexPath.row ==1) {

            UITextField *textField = [[UITextField alloc] init];
            // Set a unique tag on each text field
            textField.tag = 3+ indexPath.row;
            // Add general UITextAttributes if necessary
            textField.enablesReturnKeyAutomatically = YES;
            textField.autocorrectionType = UITextAutocorrectionTypeNo;
            textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
            [cell.contentView addSubview:textField];
        }else{

        }
    }
}
//cell.textLabel.text=@"wqewq";
//cell.textLabel.text = [[listOfsections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

[self configureCell:cell atIndexPath:indexPath];



return cell;
}

- (void)configureCell:(UITableViewCell *)theCell atIndexPath:(NSIndexPath *)indexPath {

if (indexPath.section == 0) {

    // Get the text field using the tag
    UITextField *textField = (UITextField *)[theCell.contentView viewWithTag:1+indexPath.row];
    // Position the text field within the cell bounds
    CGRect cellBounds = theCell.bounds;
    CGFloat textFieldBorder = 10.f;
    // Don't align the field exactly in the vertical middle, as the text
    // is not actually in the middle of the field.
    CGRect aRect = CGRectMake(textFieldBorder, 9.f, CGRectGetWidth(cellBounds)-(2*textFieldBorder), 31.f );

    textField.frame = aRect;

    theCell.textLabel.text = NULL;


    // Configure UITextAttributes for each field
    if(indexPath.section == 0 & indexPath.row == 0) {
        textField.placeholder = @"Name";
        textField.returnKeyType = UIReturnKeyNext;
        textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
    } else if(indexPath.section == 0 & indexPath.row == 1) {
        textField.placeholder = @"Price";
        textField.returnKeyType = UIReturnKeyNext;
        textField.keyboardType = UIKeyboardTypeNumberPad;
    }else if (indexPath.section ==0 & indexPath.row >1){
        [textField removeFromSuperview];
    }
}else if (indexPath.section == 1) {

    // Get the text field using the tag
    UITextField *textField = (UITextField *)[theCell.contentView viewWithTag:2+indexPath.row];
    // Position the text field within the cell bounds
    CGRect cellBounds = theCell.bounds;
    CGFloat textFieldBorder = 10.f;
    // Don't align the field exactly in the vertical middle, as the text
    // is not actually in the middle of the field.
    CGRect aRect = CGRectMake(textFieldBorder, 9.f, CGRectGetWidth(cellBounds)-(2*textFieldBorder), 31.f );

    textField.frame = aRect;

    // Configure UITextAttributes for each field
    if(indexPath.section == 1 & indexPath.row == 0) {
        textField.placeholder = @"Year";
        textField.returnKeyType =UIReturnKeyDone;
        textField.autocapitalizationType = UIKeyboardTypeNumberPad;
        theCell.textLabel.text = NULL;
    }else if(indexPath.section == 1 & indexPath.row != 0){
        [textField removeFromSuperview];
        theCell.textLabel.text = [[listOfsections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

    }
}else if (indexPath.section == 2) {

    // Get the text field using the tag
    UITextField *textField = (UITextField *)[theCell.contentView viewWithTag:3+indexPath.row];
    // Position the text field within the cell bounds
    CGRect cellBounds = theCell.bounds;
    CGFloat textFieldBorder = 10.f;
    // Don't align the field exactly in the vertical middle, as the text
    // is not actually in the middle of the field.
    CGRect aRect = CGRectMake(textFieldBorder, 9.f, CGRectGetWidth(cellBounds)-(2*textFieldBorder), 31.f );

    textField.frame = aRect;

    // Configure UITextAttributes for each field
    if(indexPath.section == 2 & indexPath.row == 0) {
        textField.placeholder = @"Keyword1";
        textField.returnKeyType =UIReturnKeyDone;
        textField.autocapitalizationType = UIKeyboardTypeNumberPad;
    }else if(indexPath.section == 2 & indexPath.row == 1) {
        textField.placeholder = @"Keyword2";
        textField.returnKeyType =UIReturnKeyDone;
        textField.autocapitalizationType = UIKeyboardTypeNumberPad;
    }else{}
}



}

已修正! 如果有人遇到相同的問題(工作代碼)

static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Add a UITextField
    UITextField *textField = [[UITextField alloc] init];
    // Set a unique tag on each text field
    textField.tag = 1 + indexPath.row;
    textField.delegate=self;
    // Add general UITextAttributes if necessary
    textField.enablesReturnKeyAutomatically = YES;
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    // Position the text field within the cell bounds
    CGRect cellBounds = cell.bounds;
    CGFloat textFieldBorder = 10.f;
    // Don't align the field exactly in the vertical middle, as the text
    // is not actually in the middle of the field.
    CGRect aRect = CGRectMake(textFieldBorder, 9.f, CGRectGetWidth(cellBounds)-(2*textFieldBorder), 31.f );

    textField.frame = aRect;

    textField.placeholder = @"Search for an item";
    textField.returnKeyType = UIReturnKeyDefault;
    textField.autocapitalizationType = UITextAutocapitalizationTypeWords;

        if ((indexPath.section == 0) & (indexPath.row==0)) {

            [cell.contentView addSubview:textField];
            cell.textLabel.text=NULL;
        }else{
            [textField removeFromSuperview];
            cell.textLabel.text = [[sections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        }

該表不會使單元格重疊,但是單元格確實可以重用。 您的問題是初始化單元時僅配置單元。 實際上,無論是創建新單元格還是從-dequeueReusableCellWithIdentifier:獲取舊單元格,都需要進行該配置。

也許您認為表格視圖可以保存您提供的每個單元格? 這不是它的工作原理。可重復使用的單元格意味着一個表只需要一次顯示的單元格就可以。 滾動視圖時,從頂部或底部滾動的單元將作為新單元被回收以用於新內容。 但是,您不是在設置新內容,而是顯示舊內容。

if的第一行之后添加一個右括號,以便始終執行其余代碼:

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

if (indexPath.section == 0) {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM