我想一些子视图添加到我的UITableViewCell。子视图的数量基于我的数据。当我向下滚动时,子视图消失,不再显示。将它们添加到NIB是没有选择的,因为我现在只是在运行时的子视图数量,它们对于每个单元都是不同的。动态添加子视图的UITableViewCell
什么是一个未知的数子视图在运行时添加到一个UITableViewCell的正确方法?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"DetailCell";
DetailCellTableViewCell *cell = (DetailCellTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DetailCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
NSInteger count = [self getMaxSubviews];
NSInteger y=100;
for (int i=0; i<count;i++)
{
UITextField *dataS = [[UITextField alloc] init];
dataS.frame=CGRectMake(277, y, 60, 17);
y=y+17;
dataS.tag=i+1337;
dataS.backgroundColor=[UIColor redColor];
[cell addSubview:dataS];
}
}
if (!useOrigCellFromNib) // Here I can use the original Nib created by IB
{
NSString *data = @"Some String";
[cell.data setText:data];
}
else // Use added subviews!
{
for (int i=0;i<arrS.count;i++)
{
NSManagedObject *s = [arrS objectAtIndex:i];
UITextView *dataS =[cell viewWithTag:i+1337];
dataS.text=[NSString stringWithFormat:@"%ld foo", (long)i];
[cell.data setHidden:YES];
}
}
return cell;
}
调整在哪里ü申报细胞? –
根据您的评论更新。 Fogotten粘贴声明部分的单元格。 – lueda