我有一个加载3个单元格的表。第一个单元格的标签文本为“Don”,最后两个单元格的标签文本为“Test”。
我注意到当我首先搜索“Don”时,UIButton标签是100(正确的标签#)。接下来我将搜索“Test”,第一个单元格的UIButton标签为100(应为101),第二个单元格的UIButton标签为102(正确)。
当我重新运行应用程序并首先搜索“Test”时,第一个单元格的UIButton标记为101(正确),第二个单元格的UIButton标记为102(正确)。但是当我之后搜索“Don”时,单元格的UIButton标签是101(应该是100)。
我不确定它是否仅在第一个细胞上重叠,或者我在下面提供我的细胞代码是什么。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// register cell identifier from custom cell NIB
static NSString *CellIdentifier = @"FriendCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Avatar settings
UIImageView *imvAvatar = [[UIImageView alloc] initWithFrame:CGRectMake(3, 3, 45, 45)];
[imvAvatar setImage:[UIImage imageNamed:@"btnAvatar2.png"]];
imvAvatar.layer.cornerRadius = imvAvatar.frame.size.height/2;
imvAvatar.layer.masksToBounds = YES;
//imvAvatar.layer.borderColor = [UIColor colorWithRed:59/255 green:59/255 blue:121/255 alpha:1].CGColor;
//imvAvatar.layer.borderWidth = 1.0f;
// Befriend Button settings
UIButton *btnBefriend = [[UIButton alloc] initWithFrame:CGRectMake(281, 14, 36, 22)];
[btnBefriend addTarget:self action:@selector(btnBefriendPressed:event:) forControlEvents:UIControlEventTouchUpInside];
// Collect friend info
if (tableView == self.searchDisplayController.searchResultsTableView) {
friend = [searchResults objectAtIndex:indexPath.section];
} else {
friend = [arrFriends objectAtIndex:indexPath.section];
}
NSString *user_id = (NSString *)[friend objectAtIndex:0]; // user id
NSString *username = (NSString *)[friend objectAtIndex:1]; // username
NSString *fName = (NSString *)[friend objectAtIndex:2]; // first name
NSString *lName = (NSString *)[friend objectAtIndex:3]; // last name
NSString *full_name = (NSString *)[friend objectAtIndex:4]; // full name
UIImage *picture = (UIImage *)[friend objectAtIndex:5]; // picture (img)
NSString *type = (NSString *)[friend objectAtIndex:6]; // type
NSString *arrIndex = (NSString *)[friend objectAtIndex:7]; // arrFriends index
// configure cell
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// set width depending on device orientation
cell.frame = CGRectMake(cell.frame.origin.x, cell.frame.origin.y, tableView.frame.size.width, cell.frame.size.height);
// Name settings
UILabel *lblName = [[UILabel alloc] initWithFrame:(CGRectMake(60, 3, 215, 45))];
[lblName setFont:[UIFont systemFontOfSize:14]];
// Update name, status, picture, befriend button
lblName.text = full_name; // full name
imvAvatar.image = picture; // picture (img)
if ([type isEqualToString:@""]) {
[btnBefriend setImage:[UIImage imageNamed:@"btnBefriend.png"] forState:UIControlStateNormal];
}
else {
[btnBefriend setImage:[UIImage imageNamed:@"btnBefriended.png"] forState:UIControlStateNormal];
}
// Cell subviews
imvAvatar.tag = 1;
lblName.tag = 2;
btnBefriend.tag = [arrIndex intValue]+100;
[cell.contentView addSubview:imvAvatar];
[cell.contentView addSubview:lblName];
[cell.contentView addSubview:btnBefriend];
cell.clipsToBounds = YES;
}
else {
// Make sure images, buttons and texts don't overlap
// avatar
UIImageView *imvAvatar = (UIImageView *)[cell viewWithTag:1];
imvAvatar.image = picture;
// name
UILabel *lblName = (UILabel *)[cell.contentView viewWithTag:2];
lblName.text = full_name;
// befriendbutton
UIButton *btnBefriend = (UIButton *)[cell.contentView viewWithTag:[arrIndex intValue]+100];
if ([type isEqualToString:@""]) {
[btnBefriend setImage:[UIImage imageNamed:@"btnBefriend.png"] forState:UIControlStateNormal];
}
else {
[btnBefriend setImage:[UIImage imageNamed:@"btnBefriended.png"] forState:UIControlStateNormal];
}
}
return cell;
}
我认为问题就在于:
friend = [searchResults objectAtIndex:indexPath.section];
and/or
UIButton *btnBefriend = (UIButton *)[cell.contentView viewWithTag:[arrIndex intValue]+100];
但是核心图像视图和标签文本出现了,所以我不完全确定是什么导致了这个问题。
非常感谢帮助。提前谢谢。
答案 0 :(得分:0)
来自Apple文档
-dequeueReusableCellWithIdentifier:
...
如果现有单元可以重用,则此方法调用 而是单元格的 prepareForReuse 方法。
-prepareForReuse
<强> ... 强>
tableView中的表视图委托:cellForRowAtIndexPath:应该 重复使用单元格时,请始终重置所有内容。
<强> ... 强>
修改 - 更多信息 我没有看到您在代码中明确设置标记值。请阅读Apple文档中的内容。也许这也是价值观不正确的原因。
UIView.tag
默认值为0.您可以设置此标记的值,并使用该值来标识视图&gt;稍后。
答案 1 :(得分:0)
您应该使用avatr图像和按钮以及所需的所有其他必需元素创建自定义UITableViewCell,然后重复使用此自定义单元格。如果您正在向- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
中的单元格添加元素,那么只有该单元格将被重用,而不是像您要添加到单元格中的imvAvatar
,btnBefriend
这样的ui元素,这些元素将被初始化。这也可能导致内存问题
答案 2 :(得分:0)
嘿你可以尝试以下列方式实施。由于Cell是重用的,我们需要在需要时保持cell的属性。您可以参考以下代码:
这称为:“将子视图添加到单元格的内容视图”
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CellIdentifier";
UILabel *firstNameLbl, *lastNameLbl ,*userNameLbl ;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
firstNameLbl = [[UILabel alloc]initWithFrame:CGRectMake(79, 1, 242, 33)];
firstNameLbl.tag = 500;
lastNameLbl = [[UILabel alloc]initWithFrame:CGRectMake(79, 30, 241, 20)];
lastNameLbl.tag = 501;
userNameLbl = [[UILabel alloc]initWithFrame:CGRectMake(79, 50, 242, 18)];
musicNarratedByLbl.tag = 502;
[cell.contentView addSubview: firstNameLbl];
[cell.contentView addSubview: lastNameLbl];
[cell.contentView addSubview: userNameLbl];
}else{
firstNameLbl = (UILabel *)[cell.contentView viewWithTag:500];
lastNameLbl = (UILabel* )[cell.contentView viewWithTag:501];
userNameLbl = (UILabel *)[cell.contentView viewWithTag:502];
}
// Here is the code for checking the searchresult controller or normal tableview and apply the data to the lable like as follow;
if (tableView == self.searchDisplayController.searchResultsTableView) {
//grab and apply content to each element of Search Results TableView from defined Searche array to each of the lables
}
else
{
//grab and apply content to each element of Table view from defined array to each of the lables
}
return cell;
}
有关详情,请参阅Apple referance代码段。 在该链接中,请参阅:清单5-7将子视图添加到单元格的内容视图