在 iPhone 中滚动后,披露指示器从其位置移动

发布于 2024-09-17 08:41:24 字数 637 浏览 4 评论 0原文

我面临一个奇怪的问题。在表格视图中,我有 3 个部分,其中第 1 部分只有标签和文本字段。第 2 部分和第 3 部分包含表格某些单元格上的文本字段和公开指示符。假设第 2 部分有 10 个单元格,则第 3 部分包含公开指示符和其他具有文本字段的单元格。

我的问题是,当我上下滚动视图(例如 3-4 次)时,披露指示符单元格会从该位置移动并放置在该部分中的任何位置。如果我继续向前滚动,披露指示器将再次放置到该位置。如果我删除这些披露指示器并仅放置文本字段,则一切都会运行良好。

我还尝试在 cellForRowAtIndexPath 中使用 switch case(如以下链接中所述),但没有任何帮助我......:( http://www.iphonedevsdk。 com/forum/iphone-sdk-development/13370-uitableview-cells-uilabel-problem.html

我做了很多谷歌,但无法找到解决方案。

请帮助我...

问候, 虚拟机

I am facing a strange problem. In a table view, I am having 3 sections out of which 1st section is having only labels and text fields. 2nd and 3rd section contains text fields and disclosure indicator on some cells of table.Say 2nd section is having 10 cells, then 3 contains disclosure indicator and other having text fields.

My problem is when I scroll the view up and downs, say 3-4 times, the disclosure indicator cells are shifted from there position and are placed on any location in that section. If I continue to scroll ahead, disclosure indicator are placed to there position again. If I remove those disclosure indicator and place only text fields all runs well.

I also tried using switch case in cellForRowAtIndexPath (as mentioned in following link), but nothing helping me out... :(
http://www.iphonedevsdk.com/forum/iphone-sdk-development/13370-uitableview-cells-uilabel-problem.html

I did a lot google, but not able to find a solution for this.

Please help me in this...

Regards,
VM

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

合约呢 2024-09-24 08:41:24

请找到代码供您参考。

第一部分包含 6 个单元格,第二部分包含 12 个单元格,与第三部分相同。在第二部分和第三部分中,第 4、8、9 号单元格包含披露指示符,其他单元格仅包含文本字段。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell;
NSInteger row = [indexPath row];NSLog(@"Row->%d",row);
NSInteger section = [indexPath section];NSLog(@"section->%d",section);
static NSString *CellIdentifier = @"Cell";
static NSString *BillingInfoCellIdentifier = @"BillingCell";
static NSString *DIBillingInfoCellIdentifier = @"DIBillingCell";
static NSString *BCellIdentifier = @"BCell";

switch (indexPath.section) {
    case 0:

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 25)];
    cellLabel.tag = 56;
    cellLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [cell.contentView addSubview:cellLabel];
    [cellLabel release];

        switch (indexPath.row) {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5: 
                textField = [[UITextField alloc] initWithFrame: CGRectMake(105, 12, 200, 25)];
                textField.clearsOnBeginEditing = NO;
                textField.placeholder  = [[tempArray objectAtIndex:section] objectAtIndex:row];
                [textField setDelegate:self];
                [textField addTarget:self action:@selector(textFieldDone:) forControlEvents:UIControlEventEditingDidEndOnExit];
                textField.tag = 57;
                [cell.contentView addSubview:textField];NSLog(@"in adding text fields");
                break;
        }
        break;
    case 1:
        cell = [tableView dequeueReusableCellWithIdentifier:BillingInfoCellIdentifier];
        if (cell == nil) {

            switch (indexPath.row) {
                case 0:
                case 1:
                case 2:
                case 3:
                case 5: 
                case 6:
                case 7:
                case 10:
                case 11:
                    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:BillingInfoCellIdentifier] autorelease];
                    UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 25)];
                    cellLabel.tag = 56;
                    cellLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
                    [cell.contentView addSubview:cellLabel];
                    [cellLabel release];
                    cell.accessoryType = UITableViewCellAccessoryNone;
                    textField = [[UITextField alloc] initWithFrame: CGRectMake(105, 12, 200, 25)];
                    textField.clearsOnBeginEditing = NO;
                    textField.placeholder  = [[tempArray objectAtIndex:section] objectAtIndex:row];
                    [textField setDelegate:self];
                    [textField addTarget:self action:@selector(textFieldDone:) forControlEvents:UIControlEventEditingDidEndOnExit];
                    textField.tag = 57;
                    [cell.contentView addSubview:textField];NSLog(@"in adding text fields");
                    break;
                case 4:
                case 8:
                case 9:
                    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:BillingInfoCellIdentifier] autorelease];
                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 25)];
                    cellLabel.tag = 56;
                    cellLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
                    [cell.contentView addSubview:cellLabel];
                    [cellLabel release];
                    break;
            }
            break;

        }
        break;
    case 2:
        cell = [tableView dequeueReusableCellWithIdentifier:BCellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:BCellIdentifier] autorelease];
            UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 25)];
            cellLabel.tag = 56;
            cellLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
            [cell.contentView addSubview:cellLabel];
            [cellLabel release];
        }
    break;
}

}
UILabel *cellLabel = (UILabel *)[cell.contentView viewWithTag:56];
UITextField *TF = (UITextField *)[cell.contentView viewWithTag:57];

switch (indexPath.section) {
    case 0:
        switch (indexPath.row) {
            case 0:
                cellLabel.text = @"First Name:";
                TF. placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                break;
            case 1:
                cellLabel.text = @"Last Name";//TF.text = @"Last Name:";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                NSLog(@"TF.placeholder 1->%@",[[tempArray objectAtIndex:section] objectAtIndex:row]);
                break;
            case 2:
                cellLabel.text = @"eMail";//TF.text = @"email Name:";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                NSLog(@"TF.placeholder 2->%@",[[tempArray objectAtIndex:section] objectAtIndex:row]);
                break;
            case 3:
                cellLabel.text = @"Password";//TF.text = @"pass Name:";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                NSLog(@"TF.placeholder 3->%@",[[tempArray objectAtIndex:section] objectAtIndex:row]);
                break;
            case 4:
                cellLabel.text = @"Confirm";//TF.text = @"conform Name:";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                NSLog(@"TF.placeholder 4->%@",[[tempArray objectAtIndex:section] objectAtIndex:row]);
                break;
            case 5:
                cellLabel.text = @"Phone";//TF.text = @"phone Name:";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                NSLog(@"TF.placeholder 5->%@",[[tempArray objectAtIndex:section] objectAtIndex:row]);
                break;
        }
        break;
    case 1:
        switch(indexPath.row) {
            case 0:
                cellLabel.text = @"First Name";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                break;
            case 1:
                cellLabel.text = @"Last Name";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                break;
            case 2:
                cellLabel.text = @"Phone";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                break;
            case 3:
                cellLabel.text = @"Company";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                break;
            case 5: 
                cellLabel.text = @"Address 1";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                break;
            case 6:
                cellLabel.text = @"Address 2";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                break;
            case 7:
                cellLabel.text = @"City";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                break;
            case 10:
                cellLabel.text = @"Suit";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                break;
            case 11:
                cellLabel.text = @"Zip";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                break;
            default:
                cellLabel.text = @"2 else ";
                //TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                break;
        }
        break;
    case 2:
        switch (indexPath.row) {
            case 0:
                cellLabel.text = @"Label 2";
                break;
            case 1:
                cellLabel.text = @"Label 2";
                break;
            default:
                cellLabel.text = @"3.  else Label ";
                break;
        }
        break;
    default:
        break;
}
return cell;

}

Please find the code for your reference.

First section contains 6 cells, 2nd section contains 12 and same with 3rd section. In 2nd and 3rd section cell no 4,8,9 contains disclosure indicators, other cells contains only text fields.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell;
NSInteger row = [indexPath row];NSLog(@"Row->%d",row);
NSInteger section = [indexPath section];NSLog(@"section->%d",section);
static NSString *CellIdentifier = @"Cell";
static NSString *BillingInfoCellIdentifier = @"BillingCell";
static NSString *DIBillingInfoCellIdentifier = @"DIBillingCell";
static NSString *BCellIdentifier = @"BCell";

switch (indexPath.section) {
    case 0:

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 25)];
    cellLabel.tag = 56;
    cellLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [cell.contentView addSubview:cellLabel];
    [cellLabel release];

        switch (indexPath.row) {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5: 
                textField = [[UITextField alloc] initWithFrame: CGRectMake(105, 12, 200, 25)];
                textField.clearsOnBeginEditing = NO;
                textField.placeholder  = [[tempArray objectAtIndex:section] objectAtIndex:row];
                [textField setDelegate:self];
                [textField addTarget:self action:@selector(textFieldDone:) forControlEvents:UIControlEventEditingDidEndOnExit];
                textField.tag = 57;
                [cell.contentView addSubview:textField];NSLog(@"in adding text fields");
                break;
        }
        break;
    case 1:
        cell = [tableView dequeueReusableCellWithIdentifier:BillingInfoCellIdentifier];
        if (cell == nil) {

            switch (indexPath.row) {
                case 0:
                case 1:
                case 2:
                case 3:
                case 5: 
                case 6:
                case 7:
                case 10:
                case 11:
                    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:BillingInfoCellIdentifier] autorelease];
                    UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 25)];
                    cellLabel.tag = 56;
                    cellLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
                    [cell.contentView addSubview:cellLabel];
                    [cellLabel release];
                    cell.accessoryType = UITableViewCellAccessoryNone;
                    textField = [[UITextField alloc] initWithFrame: CGRectMake(105, 12, 200, 25)];
                    textField.clearsOnBeginEditing = NO;
                    textField.placeholder  = [[tempArray objectAtIndex:section] objectAtIndex:row];
                    [textField setDelegate:self];
                    [textField addTarget:self action:@selector(textFieldDone:) forControlEvents:UIControlEventEditingDidEndOnExit];
                    textField.tag = 57;
                    [cell.contentView addSubview:textField];NSLog(@"in adding text fields");
                    break;
                case 4:
                case 8:
                case 9:
                    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:BillingInfoCellIdentifier] autorelease];
                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 25)];
                    cellLabel.tag = 56;
                    cellLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
                    [cell.contentView addSubview:cellLabel];
                    [cellLabel release];
                    break;
            }
            break;

        }
        break;
    case 2:
        cell = [tableView dequeueReusableCellWithIdentifier:BCellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:BCellIdentifier] autorelease];
            UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 25)];
            cellLabel.tag = 56;
            cellLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
            [cell.contentView addSubview:cellLabel];
            [cellLabel release];
        }
    break;
}

}
UILabel *cellLabel = (UILabel *)[cell.contentView viewWithTag:56];
UITextField *TF = (UITextField *)[cell.contentView viewWithTag:57];

switch (indexPath.section) {
    case 0:
        switch (indexPath.row) {
            case 0:
                cellLabel.text = @"First Name:";
                TF. placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                break;
            case 1:
                cellLabel.text = @"Last Name";//TF.text = @"Last Name:";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                NSLog(@"TF.placeholder 1->%@",[[tempArray objectAtIndex:section] objectAtIndex:row]);
                break;
            case 2:
                cellLabel.text = @"eMail";//TF.text = @"email Name:";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                NSLog(@"TF.placeholder 2->%@",[[tempArray objectAtIndex:section] objectAtIndex:row]);
                break;
            case 3:
                cellLabel.text = @"Password";//TF.text = @"pass Name:";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                NSLog(@"TF.placeholder 3->%@",[[tempArray objectAtIndex:section] objectAtIndex:row]);
                break;
            case 4:
                cellLabel.text = @"Confirm";//TF.text = @"conform Name:";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                NSLog(@"TF.placeholder 4->%@",[[tempArray objectAtIndex:section] objectAtIndex:row]);
                break;
            case 5:
                cellLabel.text = @"Phone";//TF.text = @"phone Name:";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                NSLog(@"TF.placeholder 5->%@",[[tempArray objectAtIndex:section] objectAtIndex:row]);
                break;
        }
        break;
    case 1:
        switch(indexPath.row) {
            case 0:
                cellLabel.text = @"First Name";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                break;
            case 1:
                cellLabel.text = @"Last Name";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                break;
            case 2:
                cellLabel.text = @"Phone";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                break;
            case 3:
                cellLabel.text = @"Company";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                break;
            case 5: 
                cellLabel.text = @"Address 1";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                break;
            case 6:
                cellLabel.text = @"Address 2";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                break;
            case 7:
                cellLabel.text = @"City";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                break;
            case 10:
                cellLabel.text = @"Suit";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                break;
            case 11:
                cellLabel.text = @"Zip";
                TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                break;
            default:
                cellLabel.text = @"2 else ";
                //TF.placeholder = [[tempArray objectAtIndex:section] objectAtIndex:row];
                break;
        }
        break;
    case 2:
        switch (indexPath.row) {
            case 0:
                cellLabel.text = @"Label 2";
                break;
            case 1:
                cellLabel.text = @"Label 2";
                break;
            default:
                cellLabel.text = @"3.  else Label ";
                break;
        }
        break;
    default:
        break;
}
return cell;

}

故事↓在人 2024-09-24 08:41:24

尝试在分配 cell 后进行所有初始化,如果之前为 nil,则不进行条件调整。

含义:

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
 }

现在单元格初始化的其余部分(它是子视图等等)

Try to do all the initialization after you allocate the cell , without conditioning if it was nil before.

Meaning :

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
 }

Now the rest of initialization of the cell (it's sub views and more)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文