当我滚动 tableview 数据时没有以正确的方式显示
when i scroll tableview data is not displaying in proper way
我正在使用 StoryBoards 构建 iOS 应用程序。
我有一个表格视图。
在 tableview Cell 中,我以编程方式创建了 5 个按钮和一个标签。
在按钮操作中,我增加了所选按钮的大小,并将其他按钮设置为默认大小。
前 4 个单元格正常工作 fine.when 我添加了新的单元格数据。
当我滚动表格视图时出现问题,单元格中的按钮和标签显示不在 order.Wrong 数据显示中。
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";
}
}
我没有找到你的单元格初始化代码,你是不是忘记在重用之前创建单元格了
cell1 = [_skillSelectionTable dequeueReusableCellWithIdentifier:Cellidentifier1 forIndexPath:indexPath];
if(!cell1){
cell1 = [[[UITableViewCell alloc] initnitWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier
}
....
我发现这个问题是关于单元正确重用的。所以,编辑你的代码,它现在可以正常工作了。
-(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";
}
}
尽情享受!!
我正在使用 StoryBoards 构建 iOS 应用程序。
我有一个表格视图。
在 tableview Cell 中,我以编程方式创建了 5 个按钮和一个标签。
在按钮操作中,我增加了所选按钮的大小,并将其他按钮设置为默认大小。
前 4 个单元格正常工作 fine.when 我添加了新的单元格数据。
当我滚动表格视图时出现问题,单元格中的按钮和标签显示不在 order.Wrong 数据显示中。
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";
}
}
我没有找到你的单元格初始化代码,你是不是忘记在重用之前创建单元格了
cell1 = [_skillSelectionTable dequeueReusableCellWithIdentifier:Cellidentifier1 forIndexPath:indexPath];
if(!cell1){
cell1 = [[[UITableViewCell alloc] initnitWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier
}
....
我发现这个问题是关于单元正确重用的。所以,编辑你的代码,它现在可以正常工作了。
-(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";
}
}
尽情享受!!