Objective c UITableView重复行的
这是我在“CellForRowatineXpath”中使用的代码Objective c UITableView重复行的,objective-c,ios,cocoa-touch,uitableview,Objective C,Ios,Cocoa Touch,Uitableview,这是我在“CellForRowatineXpath”中使用的代码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString* cellIdentifier = @"OBBirthControlMethodsTableCell"; OBCustomDetailCell *cell = (OBCusto
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* cellIdentifier = @"OBBirthControlMethodsTableCell";
OBCustomDetailCell *cell = (OBCustomDetailCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
NSLog(@"cell id - %@",cell.subviews);
CGRect frame = [tableView rectForRowAtIndexPath:0];
if(nil == cell)
{
cell = [[[OBCustomDetailCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
if (indexPath.row != 3)
{
//Setting the basic template
UIView *template = [[UIView alloc] init];
template.tag = indexPath.row+10;
NSLog(@"path index = %d",indexPath.row);
UIImageView *templateImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,
200,
frame.size.height)];
UILabel *templateLabel = [[UILabel alloc] initWithFrame:CGRectMake(templateImage.frame.size.width+20,
0,
cell.frame.size.width - templateImage.frame.size.width+20,
frame.size.height)];
[template addSubview:templateImage];
[template addSubview:templateLabel];
[cell.contentView addSubview:template];
}
}
UIView *templateView = [cell.contentView viewWithTag:indexPath.row + 10];
if (templateView)
{
NSLog(@"Gotten a templateView object");
if (indexPath.row == 0)
{
templateView.frame = frame;
for (UIView *view in templateView.subviews)
{
if ([view isKindOfClass:[UIImageView class]])
{
[(UIImageView *)view setImage:[UIImage imageNamed:@"baby.jpeg"]];
}
else if ([view isKindOfClass:[UILabel class]])
{
[(UILabel *)view setText:@"This is not working"];
}
}
}
else
{
templateView.frame = CGRectMake(0, 50,
frame.size.width,
frame.size.height);
}
}
return cell;
}
但问题是,新单元格给我的值与旧单元格的值相同,新单元格在向下滚动时会退出队列
编辑
当我们向下滚动到具有与第一个单元格相同值的新单元格时,将创建一个重复单元格
我希望仅为select行创建UIView..if indexPath.row!=3.
我希望UIView的位置在某些行中有所不同。。如果indexath.row==0
这段代码有几个问题。首先,您的问题的主要原因是:
template.tag = indexPath.row+10;
你为什么这么做?只需使用一个常量值,如10。不需要涉及索引路径,因为每个单元格的索引路径都会改变。这将导致viewWithTag:对于重复使用的单元格失败,并返回nil
其次,您不能只为indexPath.row设置模板单元格!=3,因为在某些时候,非模板单元格可能会被重用。它将没有模板视图,因此以下布局将失败。对于这两种类型的单元格,您需要使用两个重用标识符。最终产品应如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *templateCellIdentifier = @"OBBirthControlMethodsTableCell";
static NSString *otherCellIdentifier = @"OtherTableCell";
if (indexPath.row != 3) {
// Handle normal cells
OBCustomDetailCell *cell = (OBCustomDetailCell *)[tableView dequeueReusableCellWithIdentifier:templateCellIdentifier];
if (cell == nil) {
cell = [[[OBCustomDetailCell alloc] initWithStyle:UITableViewStyleDefault reuseIdentifier:templateCellIdentifier] autorelease];
// Set up template cell
}
// Handle per-cell data
} else {
// Handle special cells
OBCustomDetailCell *cell = (OBCustomDetailCell *)[tableView dequeueReusableCellWithIdentifier:otherCellIdentifier];
if (cell == nil) {
cell = [[[OBCustomDetailCell alloc] initWithStyle:UITableViewStyleDefault reuseIdentifier:otherCellIdentifier] autorelease];
// Set up other cell
}
// Handle per-cell data (not really necessary if there's only one of these)
}
}
到底是什么不起作用?文本是否没有更新?indexPath.row==3处的单独单元格是否不工作?这是什么?我已经添加了一个更清晰的问题解释,你在哪里和什么时候从单元格中删除不需要的模板视图?因为出列的单元具有此视图,并且您似乎为同一单元添加了两次或更多次模板UIView的新实例,而没有从中删除现有实例。你只是在浪费记忆,让自己头疼。为什么不针对不同类型的细胞使用不同的细胞ID来定制不同的细胞?这个解决方案很粗糙。。。