gpt4 book ai didi

ios - 向下滚动然后向上滚动后 UITableViewCells 发生变化

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:20:10 24 4
gpt4 key购买 nike

我有一个 UITableView,当我向下滚动和向上滚动时,单元格的 UILabel 中的值会发生变化。我不明白为什么会这样。

我正在使用我自己子类化的自定义 UITableViewCells。

这就是我的代码减去将所有值输入标签后的样子,因为我认为错误不存在,但在我声明 tableview 委托(delegate)方法或类似方法的方式中。

- (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;
}

如何停止单元格的重复?

最佳答案

- (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 中是否有元素:如果它有元素那么只有循环来这里。希望对你有所帮助

关于ios - 向下滚动然后向上滚动后 UITableViewCells 发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19679377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com