滚动后 UITableView 设计无法正常工作
UITableView design not worked right after Scrolling
我在我的应用程序中使用了 UITableView 并在其中使用了自定义单元格,当查看午餐时它工作得很好但是当滚动 table 并再次设置它时它就像丑陋的图像一样丑陋。
My normal tableView
My tableView after Scrolling
DetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.sellPropertyLabel.text=sellPropertyArray[indexPath.row];
cell.sellPropertyTextField.delegate=self;
if (indexPath.row==0)
{
cell.sellPropertyTextField.tag=indexPath.row;
if (_realEstateTitle)
{
cell.sellPropertyTextField.text=_realEstateTitle;
}
}
else if (indexPath.row==1)
{
cell.sellPropertyTextField.tag=indexPath.row;
if (_desOfHomeInterior)
{
cell.sellPropertyTextField.text=_desOfHomeInterior;
}
}
else if (indexPath.row==2)
{
cell.sellPropertyTextField.tag=indexPath.row;
if (_desOfGardenAndExter)
{
cell.sellPropertyTextField.text=_desOfGardenAndExter;
}
}
else if (indexPath.row==3)
{
cell.sellPropertyTextField.tag=indexPath.row;
if (_locationOfTheProperty)
{
cell.sellPropertyTextField.text=_locationOfTheProperty;
}
}
else if (indexPath.row==4)
{
cell.sellPropertyTextField.tag=indexPath.row;
if (_whyBuyThisProperty)
{
cell.sellPropertyTextField.text=_whyBuyThisProperty;
}
}
else if (indexPath.row==5)
{
//cell.selected=YES;
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.sellPropertyTextField.tag=indexPath.row;
cell.sellPropertyTextField.enabled=NO;
if (_country)
{
cell.sellPropertyTextField.text=_country;
}
}
else if (indexPath.row==6)
{
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.sellPropertyTextField.tag=indexPath.row;
cell.sellPropertyTextField.enabled=NO;
if (_city)
{
cell.sellPropertyTextField.text=_city;
}
}
else if (indexPath.row==7)
{
cell.sellPropertyTextField.enabled=NO;
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.sellPropertyTextField.tag=indexPath.row;
if (_area)
{
cell.sellPropertyTextField.text=_area;
}
}
else if (indexPath.row==8)
{
cell.sellPropertyTextField.enabled=NO;
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.sellPropertyTextField.tag=indexPath.row;
if (_type)
{
cell.sellPropertyTextField.text=_type;
}
//cell.addAlertTextField.keyboardType=UIKeyboardTypePhonePad;
}
else if (indexPath.row==9)
{
cell.sellPropertyTextField.enabled=NO;
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.sellPropertyTextField.tag=indexPath.row;
if (_pool)
{
cell.sellPropertyTextField.text=_pool;
}
}
else if (indexPath.row==10)
{
cell.sellPropertyTextField.enabled=NO;
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.sellPropertyTextField.tag=indexPath.row;
if (_status)
{
cell.sellPropertyTextField.text=_status;
}
}
else if (indexPath.row==11)
{
cell.sellPropertyTextField.tag=indexPath.row;
if (_bedrooms)
{
cell.sellPropertyTextField.text=_bedrooms;
}
}
else if (indexPath.row==12)
{
cell.sellPropertyTextField.tag=indexPath.row;
if (_bathrooms)
{
cell.sellPropertyTextField.text=_bathrooms;
}
}
else if (indexPath.row==13)
{
cell.sellPropertyTextField.tag=indexPath.row;
if (_livingSpaceSqm)
{
cell.sellPropertyTextField.text=_livingSpaceSqm;
}
}
else if (indexPath.row==14)
{
cell.sellPropertyTextField.tag=indexPath.row;
if (_landSqm)
{
cell.sellPropertyTextField.text=_landSqm;
}
}
else if (indexPath.row==15)
{
cell.sellPropertyTextField.tag=indexPath.row;
if (_price)
{
cell.sellPropertyTextField.text=_price;
}
}
else if (indexPath.row==16)
{
//cell.addAlertTextField.enabled=NO;
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.sellPropertyTextField.tag=indexPath.row;
cell.sellPropertyTextField.enabled=NO;
if (_currency)
{
cell.sellPropertyTextField.text=_currency;
}
}
return cell;
}
问题是您对单元格使用的是相同的 reuseIdentifier
。请分享你的 cellForRowAtIndexPath
方法,但我相信你有一些像 "ID"
这样的字符串。您有两种具有不同配置的单元格,因此您也应该使用不同的标识符。例如 "ID1"
表示第一类单元格,"ID2"
表示其他单元格。
This simple example should fix the problem
NSString *identifier = nil;
NSInteger row = indexPath.row;
if ((row >= 5 && row <= 10) || row == 16) {
identifier = @"CELLID1";
}
else {
identifier = @"CELLID2";
}
DetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
....
或更简单的解决方案是在您的案例
的方法开头清理 accessoryType
DetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
您需要检查单元格,因为它们正在重复使用。
你的代码结构有点难以修改,有时switch case会更好。您可以在返回单元格之前将其添加到底部:
switch(indexPath.row){
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 16:
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
break;
default:
cell.accessoryType = UITableViewCellAccessoryNone;
break;
}
return cell;
我在我的应用程序中使用了 UITableView 并在其中使用了自定义单元格,当查看午餐时它工作得很好但是当滚动 table 并再次设置它时它就像丑陋的图像一样丑陋。 My normal tableView My tableView after Scrolling
DetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.sellPropertyLabel.text=sellPropertyArray[indexPath.row];
cell.sellPropertyTextField.delegate=self;
if (indexPath.row==0)
{
cell.sellPropertyTextField.tag=indexPath.row;
if (_realEstateTitle)
{
cell.sellPropertyTextField.text=_realEstateTitle;
}
}
else if (indexPath.row==1)
{
cell.sellPropertyTextField.tag=indexPath.row;
if (_desOfHomeInterior)
{
cell.sellPropertyTextField.text=_desOfHomeInterior;
}
}
else if (indexPath.row==2)
{
cell.sellPropertyTextField.tag=indexPath.row;
if (_desOfGardenAndExter)
{
cell.sellPropertyTextField.text=_desOfGardenAndExter;
}
}
else if (indexPath.row==3)
{
cell.sellPropertyTextField.tag=indexPath.row;
if (_locationOfTheProperty)
{
cell.sellPropertyTextField.text=_locationOfTheProperty;
}
}
else if (indexPath.row==4)
{
cell.sellPropertyTextField.tag=indexPath.row;
if (_whyBuyThisProperty)
{
cell.sellPropertyTextField.text=_whyBuyThisProperty;
}
}
else if (indexPath.row==5)
{
//cell.selected=YES;
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.sellPropertyTextField.tag=indexPath.row;
cell.sellPropertyTextField.enabled=NO;
if (_country)
{
cell.sellPropertyTextField.text=_country;
}
}
else if (indexPath.row==6)
{
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.sellPropertyTextField.tag=indexPath.row;
cell.sellPropertyTextField.enabled=NO;
if (_city)
{
cell.sellPropertyTextField.text=_city;
}
}
else if (indexPath.row==7)
{
cell.sellPropertyTextField.enabled=NO;
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.sellPropertyTextField.tag=indexPath.row;
if (_area)
{
cell.sellPropertyTextField.text=_area;
}
}
else if (indexPath.row==8)
{
cell.sellPropertyTextField.enabled=NO;
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.sellPropertyTextField.tag=indexPath.row;
if (_type)
{
cell.sellPropertyTextField.text=_type;
}
//cell.addAlertTextField.keyboardType=UIKeyboardTypePhonePad;
}
else if (indexPath.row==9)
{
cell.sellPropertyTextField.enabled=NO;
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.sellPropertyTextField.tag=indexPath.row;
if (_pool)
{
cell.sellPropertyTextField.text=_pool;
}
}
else if (indexPath.row==10)
{
cell.sellPropertyTextField.enabled=NO;
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.sellPropertyTextField.tag=indexPath.row;
if (_status)
{
cell.sellPropertyTextField.text=_status;
}
}
else if (indexPath.row==11)
{
cell.sellPropertyTextField.tag=indexPath.row;
if (_bedrooms)
{
cell.sellPropertyTextField.text=_bedrooms;
}
}
else if (indexPath.row==12)
{
cell.sellPropertyTextField.tag=indexPath.row;
if (_bathrooms)
{
cell.sellPropertyTextField.text=_bathrooms;
}
}
else if (indexPath.row==13)
{
cell.sellPropertyTextField.tag=indexPath.row;
if (_livingSpaceSqm)
{
cell.sellPropertyTextField.text=_livingSpaceSqm;
}
}
else if (indexPath.row==14)
{
cell.sellPropertyTextField.tag=indexPath.row;
if (_landSqm)
{
cell.sellPropertyTextField.text=_landSqm;
}
}
else if (indexPath.row==15)
{
cell.sellPropertyTextField.tag=indexPath.row;
if (_price)
{
cell.sellPropertyTextField.text=_price;
}
}
else if (indexPath.row==16)
{
//cell.addAlertTextField.enabled=NO;
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.sellPropertyTextField.tag=indexPath.row;
cell.sellPropertyTextField.enabled=NO;
if (_currency)
{
cell.sellPropertyTextField.text=_currency;
}
}
return cell;
}
问题是您对单元格使用的是相同的 reuseIdentifier
。请分享你的 cellForRowAtIndexPath
方法,但我相信你有一些像 "ID"
这样的字符串。您有两种具有不同配置的单元格,因此您也应该使用不同的标识符。例如 "ID1"
表示第一类单元格,"ID2"
表示其他单元格。
This simple example should fix the problem
NSString *identifier = nil;
NSInteger row = indexPath.row;
if ((row >= 5 && row <= 10) || row == 16) {
identifier = @"CELLID1";
}
else {
identifier = @"CELLID2";
}
DetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
....
或更简单的解决方案是在您的案例
的方法开头清理accessoryType
DetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
您需要检查单元格,因为它们正在重复使用。
你的代码结构有点难以修改,有时switch case会更好。您可以在返回单元格之前将其添加到底部:
switch(indexPath.row){
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 16:
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
break;
default:
cell.accessoryType = UITableViewCellAccessoryNone;
break;
}
return cell;