当我滚动tableview数据没有以适当的方式显示

时间:2015-04-23 09:16:02

标签: ios uitableview nsindexpath tableviewcell

我正在使用StoryBoards构建iOS App。

我有一个tableview。

在tableview Cell中,我以编程方式创建了5个按钮和一个标签。

在按钮操作中,我增加所选按钮的大小,并将其他按钮设为默认大小。

对于前4个细胞,它工作正常。当我添加新的细胞数据时。

当我滚动桌面视图时,问题就出现了,单元格中的按钮和标签显示的顺序不正常。显示的数据错误。

tableview的单元格状态正在发生变化。

这是我的代码。

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

    static NSString *Cellidentifier1 = @"identifier";

    cell1 = [_skillSelectionTable dequeueReusableCellWithIdentifier:Cellidentifier1 forIndexPath:indexPath];

    cell1.selectionStyle = UITableViewCellSelectionStyleNone;

 first[indexPath.row] = [UIButton buttonWithType:UIButtonTypeCustom];

            first[indexPath.row].frame = CGRectMake(75, 40, 40, 20);

            [first[indexPath.row] setTitle:@"first1" forState:UIControlStateNormal];

            [first[indexPath.row] setTitleColor:[UIColor colorWithRed:0.0/255.0f green:210.0/255.0f blue:186.0/255.0f alpha:1.0] forState:UIControlStateNormal];



            first[indexPath.row].backgroundColor=[UIColor colorWithRed:0.0/255.0f green:210.0/255.0f blue:186.0/255.0f  alpha:1.0];

            [first[indexPath.row] addTarget:self action:@selector(increaseAction:) forControlEvents:UIControlEventTouchUpInside];

            first[indexPath.row].tag = indexPath.row;

            [cell1.contentView addSubview: first
[indexPath.row]];

           Second[indexPath.row] = [UIButton buttonWithType:UIButtonTypeCustom];

            Second[indexPath.row].frame = CGRectMake(120, 40, 40, 20);

            [Second[indexPath.row] setTitle:@"Second1
" forState:UIControlStateNormal];

            [Second[indexPath.row] setTitleColor:[UIColor colorWithRed:0.0/255.0f green:190.0/255.0f blue:176.0/255.0f alpha:1.0] forState:UIControlStateNormal];

            Second[indexPath.row].backgroundColor=[UIColor colorWithRed:0.0/255.0f green:190.0/255.0f blue:176.0/255.0f  alpha:1.0];

            [Second[indexPath.row] addTarget:self action:@selector(increaseAction:) forControlEvents:UIControlEventTouchUpInside];

            Second[indexPath.row].tag = indexPath.row;

            [cell1.contentView addSubview:second[indexPath.row]];

           third[indexPath.row] = [UIButton buttonWithType:UIButtonTypeCustom];

            third[indexPath.row].frame = CGRectMake(160, 35, 50, 30);

            [third[indexPath.row] setTitle:@"third 
1" forState:UIControlStateNormal];

            [third[indexPath.row] setTitleColor:[UIColor colorWithRed:0.0/255.0f green:180.0/255.0f blue:166.0/255.0f alpha:1.0] forState:UIControlStateNormal];

            third[indexPath.row].backgroundColor=[UIColor colorWithRed:0.0/255.0f green:180.0/255.0f blue:166.0/255.0f  alpha:1.0];

            [third[indexPath.row] addTarget:self action:@selector(increaseAction:) forControlEvents:UIControlEventTouchUpInside];

            third[indexPath.row].tag = indexPath.row;

            [cell1.contentView addSubview: third
[indexPath.row]];

            label[indexPath.row] = [[UILabel alloc]initWithFrame:CGRectMake(100, 69, 200, 21)];

             label[indexPath.row].text=@"second";

   label[indexPath.row].textAlignment=NSTextAlignmentCenter;

[cell1.contentView addSubview:label[indexPath.row]];

    return cell1;

}


-(IBAction)increaseAction:(id)sender

{

    UIButton *button = (UIButton *) sender;

    if ([[button currentTitle] isEqualToString:@“First1"])

    {

       First[button.tag].frame = CGRectMake(70, 35, 50, 30);

        second[button.tag].frame = CGRectMake(120, 40, 40, 20);

       third[button.tag].frame = CGRectMake(165, 40, 40, 20);

        label[button.tag].text=@"First";

}

    if ([[button currentTitle] isEqualToString:@"second1"])

    {

        First[button.tag].frame = CGRectMake(75, 40, 40, 20);

        second[button.tag].frame = CGRectMake(115, 35, 50, 30);

        third[button.tag].frame = CGRectMake(165, 40, 40, 20);

        label[button.tag].text=@"second";

    }

    if ([[button currentTitle] isEqualToString:@"third1"])

    {

        First[button.tag].frame = CGRectMake(75, 40, 40, 20);

        second[button.tag].frame = CGRectMake(120, 40, 40, 20);

        third[button.tag].frame = CGRectMake(160, 35, 50, 30);

        label[button.tag].text=@"third";

}

}

2 个答案:

答案 0 :(得分:0)

我没有找到您的单元格的初始化代码,您是否忘记在重复使用之前创建单元格

 cell1 = [_skillSelectionTable dequeueReusableCellWithIdentifier:Cellidentifier1 forIndexPath:indexPath];
if(!cell1){
cell1 = [[[UITableViewCell alloc]  initnitWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier
}
....

答案 1 :(得分:0)

我发现这个问题是关于细胞重用的。所以,现在编辑你的代码及其工作正常。

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

NSString * Cellidentifier1 = [NSString
                                    stringWithFormat:@"identifier %ld",(long)indexPath.row];

    cell1 = [_skillSelectionTable dequeueReusableCellWithIdentifier:Cellidentifier1];

    cell1.selectionStyle = UITableViewCellSelectionStyleNone;

 first[indexPath.row] = [UIButton buttonWithType:UIButtonTypeCustom];

            first[indexPath.row].frame = CGRectMake(75, 40, 40, 20);

            [first[indexPath.row] setTitle:@"first1" forState:UIControlStateNormal];

            [first[indexPath.row] setTitleColor:[UIColor colorWithRed:0.0/255.0f green:210.0/255.0f blue:186.0/255.0f alpha:1.0] forState:UIControlStateNormal];



            first[indexPath.row].backgroundColor=[UIColor colorWithRed:0.0/255.0f green:210.0/255.0f blue:186.0/255.0f  alpha:1.0];

            [first[indexPath.row] addTarget:self action:@selector(increaseAction:) forControlEvents:UIControlEventTouchUpInside];

            first[indexPath.row].tag = indexPath.row;

            [cell1.contentView addSubview: first
[indexPath.row]];

           Second[indexPath.row] = [UIButton buttonWithType:UIButtonTypeCustom];

            Second[indexPath.row].frame = CGRectMake(120, 40, 40, 20);

            [Second[indexPath.row] setTitle:@"Second1
" forState:UIControlStateNormal];

            [Second[indexPath.row] setTitleColor:[UIColor colorWithRed:0.0/255.0f green:190.0/255.0f blue:176.0/255.0f alpha:1.0] forState:UIControlStateNormal];

            Second[indexPath.row].backgroundColor=[UIColor colorWithRed:0.0/255.0f green:190.0/255.0f blue:176.0/255.0f  alpha:1.0];

            [Second[indexPath.row] addTarget:self action:@selector(increaseAction:) forControlEvents:UIControlEventTouchUpInside];

            Second[indexPath.row].tag = indexPath.row;

            [cell1.contentView addSubview:second[indexPath.row]];

           third[indexPath.row] = [UIButton buttonWithType:UIButtonTypeCustom];

            third[indexPath.row].frame = CGRectMake(160, 35, 50, 30);

            [third[indexPath.row] setTitle:@"third 
1" forState:UIControlStateNormal];

            [third[indexPath.row] setTitleColor:[UIColor colorWithRed:0.0/255.0f green:180.0/255.0f blue:166.0/255.0f alpha:1.0] forState:UIControlStateNormal];

            third[indexPath.row].backgroundColor=[UIColor colorWithRed:0.0/255.0f green:180.0/255.0f blue:166.0/255.0f  alpha:1.0];

            [third[indexPath.row] addTarget:self action:@selector(increaseAction:) forControlEvents:UIControlEventTouchUpInside];

            third[indexPath.row].tag = indexPath.row;

            [cell1.contentView addSubview: third
[indexPath.row]];

            label[indexPath.row] = [[UILabel alloc]initWithFrame:CGRectMake(100, 69, 200, 21)];

             label[indexPath.row].text=@"second";

   label[indexPath.row].textAlignment=NSTextAlignmentCenter;

[cell1.contentView addSubview:label[indexPath.row]];

    return cell1;

}


-(IBAction)increaseAction:(id)sender

{

    UIButton *button = (UIButton *) sender;

    if ([[button currentTitle] isEqualToString:@“First1"])

    {

       First[button.tag].frame = CGRectMake(70, 35, 50, 30);

        second[button.tag].frame = CGRectMake(120, 40, 40, 20);

       third[button.tag].frame = CGRectMake(165, 40, 40, 20);

        label[button.tag].text=@"First";

}

    if ([[button currentTitle] isEqualToString:@"second1"])

    {

        First[button.tag].frame = CGRectMake(75, 40, 40, 20);

        second[button.tag].frame = CGRectMake(115, 35, 50, 30);

        third[button.tag].frame = CGRectMake(165, 40, 40, 20);

        label[button.tag].text=@"second";

    }

    if ([[button currentTitle] isEqualToString:@"third1"])

    {

        First[button.tag].frame = CGRectMake(75, 40, 40, 20);

        second[button.tag].frame = CGRectMake(120, 40, 40, 20);

        third[button.tag].frame = CGRectMake(160, 35, 50, 30);

        label[button.tag].text=@"third";

}

}

享受!!