这是我在“cellForRowAtIndexPath”中使用的代码
- (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 rows() ..if (indexPath.row != 3) 创建 UIView
- 我希望 UIView 的位置在某些行中有所不同.. if (indexPath.row == 0)