Ios Tableview单元格值在滚动后更改
我正在Ios Tableview单元格值在滚动后更改,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在if(cell==nil)条件内设置单元格的占位符 但是,当我滚动表格时,占位符值会互换。请帮忙 这是cellforrowatinexpath中的代码:请告诉我我错在哪里了 static NSString *cellIdentifierCell = @"MTRegisterTableViewCell"; cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierCell]; if (c
if(cell==nil)
条件内设置单元格的占位符
但是,当我滚动表格时,占位符值会互换。请帮忙
这是cellforrowatinexpath
中的代码:请告诉我我错在哪里了
static NSString *cellIdentifierCell = @"MTRegisterTableViewCell";
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierCell];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:[MTUtility
setXibWithName:cellIdentifierCell] owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
if(indexPath.row==6)
{
if (cell.txtRegister.text.length == 0) {
if (cell.txtRegister.placeholder.length == 0) {
cell.txtRegister.placeholder=[self.profileTitlesArray
objectAtIndex:indexPath.row-1];
}
}
NSLog(@"Placeholder :%@ && iINDEXPATH.ROW
:%ld",cell.txtRegister.placeholder,(long)indexPath.row);
}
else
{
if (cell.txtRegister.text.length == 0) {
if (cell.txtRegister.placeholder.length == 0) {
cell.txtRegister.placeholder=[self.profileTitlesArray
objectAtIndex:indexPath.row];
}
}
}
}
if(indexPath.row==5)
{
static NSString *cellIdentifierCell = @"MTRegisterDropdownCell";
MTRegisterDropdownCell *cellDrop = [tableView
dequeueReusableCellWithIdentifier:cellIdentifierCell];
if (cellDrop == nil)
{
NSArray *nib;
nib = [[NSBundle mainBundle] loadNibNamed:[MTUtility
setXibWithName:cellIdentifierCell] owner:self options:nil];
cellDrop = (MTRegisterDropdownCell *)[nib objectAtIndex:0];
[cellDrop configureCell:nil];
}
self.countryCell=cellDrop;
cellDrop.clipsToBounds=NO;
self.tblDropdown.clipsToBounds=NO;
[cellDrop.btnCountry addTarget:self action:@selector(btnCountryClicked:)
forControlEvents:UIControlEventTouchUpInside];
[cellDrop.btnState addTarget:self action:@selector(ButtonStateClicked:)
forControlEvents:UIControlEventTouchUpInside];
if(self.strSelectedCountry)
{
[cellDrop.txtCountry setText:self.strSelectedCountry];
}
else{
if ([self.countriesArray containsObject:kDefaultCountry]) {
[cellDrop.txtCountry setText:kDefaultCountry];
NSInteger index = [self.countriesArray
indexOfObject:kDefaultCountry];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index
inSection:0];
self.tblDropdown.tag=TABLECOUNTRYTAG;
[self tableView:self.tblDropdown didSelectRowAtIndexPath:indexPath];
}
}
if(self.strSelectedState)
{
[cellDrop.txtState setText:self.strSelectedState];
}
return cellDrop;
}
else
{
if (cell.txtRegister.text.length == 0) {
NSLog(@"Placeholder :%@",cell.txtRegister.placeholder);
if ([cell.txtRegister.placeholder isEqualToString:@"City"]) {
if (self.strSelectedCity) {
cell.txtRegister.text = self.strSelectedCity;
}
else
cell.txtRegister.text=[self.profileTitlesArray
objectAtIndex:indexPath.row];
NSLog(@"profileTitlesArray :%@",self.profileTitlesArray);
NSLog(@"indexpath.Row :%ld && cell TEXT :%@",
(long)indexPath.row,cell.txtRegister.text);
BaseButton *aBtn = [[BaseButton alloc]
initWithFrame:CGRectMake(cell.txtRegister.frame.origin.x-50,
cell.txtRegister.frame.origin.y-10, cell.txtRegister.frame.size.width+50,
cell.txtRegister.frame.size.height+50)];
[aBtn setBackgroundColor:[UIColor clearColor]];
[cell.txtRegister addSubview:aBtn];
[aBtn addTarget:self action:@selector(btnCitiesClicked:)
forControlEvents:UIControlEventTouchUpInside];
self.cityCelll = cell;
cell.clipsToBounds = NO;
}
}
else{
if ([cell.txtRegister.placeholder isEqualToString:@"City"]) {
if (self.strSelectedCity) {
cell.txtRegister.text = self.strSelectedCity;
}
}
}
}
[cell configureCell:registerModel];
return cell;
}
我没有读完整的代码,但听起来在重新使用单元格之前,您需要重置占位符,它们不需要在scroll上重新创建,这毕竟是表视图的观点
看。它是在退出/重用现有表单元格时调用的,因此从技术上讲,当您调用-[UITableView-dequeueReusableCellWithIdentifier:][/code>(您需要将UITableViewCell
子类化时,它会进一步组织您的代码)。我最终使用以下行解决了这个问题:
NSString *cellIdentifierCell = [NSString
stringWithFormat:@"MTRegisterTableViewCell %ld",(long)indexPath.row];
cell = (MTRegisterTableViewCell *)[tableView
dequeueReusableCellWithIdentifier:cellIdentifierCell];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:[MTUtility
setXibWithName:@"MTRegisterTableViewCell"] owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
所以你根据单元格是否为零来设置一个值?TableViews循环使用单元格,因此在“第一批”之后,即首次加载单元格时在屏幕上看到的内容将不再为零…显示CellForRowatineXpath:
code…..请查看代码您没有复制整个CellForRowatineXpath:
方法…为每个单元格分配“cellIdentifierCell”的不同值。这可能有用