我有一个UITableView,当我向下滚动并向后滚动时,值在单元格的UILabel中发生变化。我无法弄清楚为什么会这样。
我正在使用我自己子类化的自定义UITableViewCells。
这是我的代码看起来像减去输入标签中的所有值,因为我认为错误不在那里,但在我声明tableview委托方法或类似的东西的方式。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (sortedItemsArray == nil) {
return 0;
} else
return [sortedItemsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomallCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomallCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.selectionStyle = UITableViewCellSelectionStyleGray;
if ([sortedItemsArray count] > 0) {
currentallDictionary = [sortedItemsArray objectAtIndex:indexPath.row];
NSNumber *tempDU = [currentallDictionary objectForKey:@"DU"];
NSInteger myInteger = [tempDU integerValue];
if (myInteger == 0) {
// NSLog(@"%@", currentallDictionary);
//assign vals to labels
NSString *areaString = [currentallDictionary objectForKey:@"area"];
if ((NSNull *) areaString != [NSNull null]) {
cell.areaLabel.text = areaString;
} else {
cell.areaLabel.text = @" ";
}
NSString *stageString = [currentallDictionary objectForKey:@"stage"];
if ((NSNull *) stageString != [NSNull null]) {
cell.stageLabel.text = stageString;
} else {
cell.stageLabel.text = @" ";
}
NSString *floorLabelString = [currentallDictionary objectForKey:@"floorNo"];
if ((NSNull *) floorLabelString != [NSNull null]) {
cell.floorLabel.text = floorLabelString;
} else {
cell.floorLabel.text = @" ";
}
NSString *floorDescLabelString = [currentallDictionary objectForKey:@"floorDesc"];
if ((NSNull *) floorDescLabelString != [NSNull null]) {
cell.floorDescLabel.text = floorDescLabelString;
} else {
cell.floorDescLabel.text = @" ";
}
//Buttons
tDxStateAString = [currentallDictionary objectForKey:@"tDxStateA"];
tDxStateBString = [currentallDictionary objectForKey:@"tDxStateB"];
tHasDxString = [currentallDictionary objectForKey:@"tHasDx"];
if ([tHasDxString isEqualToString:@"T"]) {
tDxQtyString = [currentallDictionary objectForKey:@"tDxQty"];
if ((NSNull *) tDxQtyString != [NSNull null]) {
cell.quantityALabel.text = tDxQtyString;
if (([tDxStateAString isEqualToString:@"T"]) && ([tDxStateBString isEqualToString:@"W"])) {
UIImage *checkedOnImage = [UIImage imageNamed:@"CheckedOn.png"];
[cell.DxfitButtonImage setImage:checkedOnImage forState:UIControlStateNormal];
} else if (([tDxStateAString isEqualToString:@"T"]) && ([tDxStateBString isEqualToString:@"R"])) {
UIImage *checkedOnDisabledImage = [UIImage imageNamed:@"CheckedOnDisabled.png"];
[cell.DxfitButtonImage setImage:checkedOnDisabledImage forState:UIControlStateNormal];
}else {
UIImage *checkedOffImage = [UIImage imageNamed:@"CheckedOff.png"];
[cell.DxfitButtonImage setImage:checkedOffImage forState:UIControlStateNormal];
cell.DxfitButtonImage.userInteractionEnabled = YES;
}
} else {
cell.quantityALabel.text = @" ";
cell.DxfitButtonImage.userInteractionEnabled = NO;
}
}
tStateAString = [currentallDictionary objectForKey:@"tStateA"];
tStateBString = [currentallDictionary objectForKey:@"tStateB"];
tHasInsString = [currentallDictionary objectForKey:@"tHasIns"];
if ([tHasInsString isEqualToString:@"T"]) {
tInsQtyString = [currentallDictionary objectForKey:@"tInsQty"];
if ((NSNull *) tInsQtyString != [NSNull null]) {
cell.quantityBLabel.text = tInsQtyString;
if (([tStateAString isEqualToString:@"T"]) && ([tStateBString isEqualToString:@"W"])) {
UIImage *checkedOnImage = [UIImage imageNamed:@"CheckedOn.png"];
[cell.allButtonImage setImage:checkedOnImage forState:UIControlStateNormal];
} else if (([tStateAString isEqualToString:@"T"]) && ([tStateBString isEqualToString:@"R"])) {
UIImage *checkedOnDisabledImage = [UIImage imageNamed:@"CheckedOnDisabled.png"];
[cell.allButtonImage setImage:checkedOnDisabledImage forState:UIControlStateNormal];
} else {
UIImage *checkedOffImage = [UIImage imageNamed:@"CheckedOff.png"];
[cell.allButtonImage setImage:checkedOffImage forState:UIControlStateNormal];
}
} else {
cell.quantityBLabel.text = @" ";
cell.allButtonImage.userInteractionEnabled = NO;
}
}
}
}
return cell;
}
任何帮助阻止细胞复制的帮助都将不胜感激。
答案 0 :(得分:1)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomInstallCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomInstallCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
UILabel *lbl_name = [[UILabel alloc]initWithFrame:CGRectMake(10, 7, 280, 25)];
lbl_name.tag = 1000;
[lbl_name setFont:[UIFont fontWithName:@"Helvetica" size:16.0f]];
lbl_name.lineBreakMode = UILineBreakModeWordWrap;
lbl_name.numberOfLines = 0;
[lbl_name setBackgroundColor:[UIColor clearColor]];
[lbl_name sizeToFit];
[cell.contentView addSubview:lbl_name];
//Create all your Labels here and set the tag
}
//Access your lbl by the tag here
UILabel *lbl_name = (UILabel *)[cell.contentView viewWithTag:1000];
[lbl_name setText:[yourArray objectAtIndex:indexPath.row]];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
return cell;
}
尝试这样..然后不需要检查数组在cellForRowAtIndexPath中有元素:如果它有元素那么只有循环来到这里。我希望它会对你有所帮助
答案 1 :(得分:0)
在您填写所有标签的地方",我不确定您想要实现的目标。
如果你的自定义单元格中有很多UILabel要使用sortedItemsArray
填充,那么在
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
你不应该归还[sortedItemsArray count]
。
如果您的自定义单元格中有一个UILabel,并且您尝试根据索引使用sortedItemsArray
填充标签,那么您需要做的只是
cell.yourCustomLabel = sortedItemsArray [indexPath.row];
如果您的案例不属于上述情况,请附上更多代码以进行详细说明。
答案 2 :(得分:0)
如果您有子视图uitableview单元格,请确保您在自定义单元格xib的atttrib utes检查器中提供了“CellIdentifier”。并在indexpath处更改行的单元格,如下所示
static NSString * cellIdentifier = @“cell”;
CustomInstallCell * cell = (CustomInstallCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomInstallCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
答案 3 :(得分:0)
UITableViewController使用可重用的单元格。控制器创建有限数量的单元格和滚动只是覆盖内容。这可能导致细胞中某些特性的重复。为避免重复,您可以使用UITableViewDelegate的方法tableView:willDisplayCell:forRowAtIndexPath:
。在此方法中,检查重复或不正确的内容并正确设置。
答案 4 :(得分:0)
UITableViewCell
在滚动表视图时重用单元格,基于CellIdentifier
重用单元格,在表格视图中我们仅创建新视图cell==nil
我们在表视图中重用单元格,< / p>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomInstallCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomInstallCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
if ([sortedItemsArray count] > 0) {
UIView *view=[CustomInstallCell viewWithTag:your view tag];
//view is sub view of CustomInstallCell
}
}
return cell;
}
答案 5 :(得分:0)
static NSString *CellIdentifier;
if(IPAD)
CellIdentifier=[NSString stringWithFormat:@"EditCell_ipad%d",indexPath.row];
else
CellIdentifier=[NSString stringWithFormat:@"EditCell_iphone%d",indexPath.row];
CustomallCell *cell=(CustomallCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"CustomallCell_TablviewCell" owner:self options:nil];
if(IPAD)
cell = (CustomallCell*)[nib objectAtIndex:0];
else
cell = (CustomallCell *)[nib objectAtIndex:1];
}
我正在为每个单元格使用唯一的Idetifier,因此没有机会重复/覆盖数据单元格问题。滚动后就像单元格的上一个状态一样,这个代码不会发生
答案 6 :(得分:0)
NSString *CellIdentifier = [NSString stringWithFormat:@"cell%d",indexPath.row];