我在uiviewcontroller中有一个uitableview,我在viewload事件中创建了一个scrollview。 我将它添加到tableview的第一个单元格。但我滚动了tableview它在5个单元格传递后显示了多个scrollview。
这是代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"customCell";
DetailCellViewController *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nibObjects =[[NSBundle mainBundle] loadNibNamed:@"DetailCellView" owner:nil options:nil];
for (id currentObject in nibObjects)
{
if ([currentObject isKindOfClass:[DetailCellViewController class]])
{
cell = (DetailCellViewController *) currentObject;
}
}
}
if (indexPath.row==0) {
[cell.contentView addSubview:scrollView];
}
else {
NSMutableDictionary *dictionary=[catData objectAtIndex:indexPath.row-1];
NSString *title =[dictionary objectForKey:@"title"]];
[cell.catTitle setText:title];
}
return cell;
}
我应该在哪个事件中添加&删除scrollview?
答案 0 :(得分:2)
我的猜测是你得到一个已经包含UIScrollView的出列UITableViewCell。如果你真的关心分离细胞类型,我建议你设置它,这样你至少有两个CellIdentifier字符串。 (有些时候我设置了一个UITableView来处理4种以上不同的细胞类型;一旦你超越了一种细胞类型,它就差不多了。)
我建议的解决方案: (见下面的代码说明)
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"bodyCell";
static NSString *HeaderIdentifier = @"headerCell";
UITableViewCell *cell;
// I break this up into 3 sections
// #1. Try to dequeue a cell
// #2. Create a new cell (if needed)
// #3. Set up the cell I've created
// Step 1: Try to dequeue a cell
if ([indexPath section] == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:HeaderIdentifier];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
// At this point, we may or may not have a cell to use,
// so we check for the cell's value being equal to 'nil'
// and create a new cell if it is
// Step 2: Create a new cell (if needed)
if (cell == nil) {
// Again, here we check for section to determine
// what kind of cell we want
if ([indexPath section] == 0) {
// We have the "header"/first cell
// Option 1
cell = [[ScrollViewTableViewCell alloc] init];
// Option 2 (this assumes you've got a xib named
// ScrollingTableViewCell along with a class property
// named headerCell and have properly wired it up in
// Interface Builder)
[[NSBundle mainBundle] loadNibNamed:@"ScrollingTableViewCell"
owner:self
options:nil];
cell = [self headerCell];
[self setHeaderCell:nil];
} else {
// We have a "body" cell (anything other than the first cell)
// Option 1
cell = [[BodyTableViewCell alloc] init];
// Option 2 (again, assuming you've set things up properly)
[[NSBundle mainBundle] loadNibNamed:@"BodyTableViewCell"
owner:self
options:nil];
cell = [self bodyCell];
[self setBodyCell:nil];
}
}
// At this point, whether dequeued or created
// new, 'cell' should be populated
// Again, we check for section and set up the cell as appropriate
if ([indexPath section] == 0) {
// Set up the header (UIScrollView) cell as appropriate
// This is where you would add the UISCrollView to your cell
// (if you haven't set up the UIScrollView through Interface Builder)
} else {
// Set up the "body" cell as appropriate
}
return cell;
}
注意:我 HIGHLY 建议使用上面的选项2。到目前为止,我在使用自定义/非标准UITableViewCells时发现的最好结果是使我自己的UITableViewCell子类和xib同时使用它。以下是步骤:
如果您按照上述说明操作,您应该能够使用上面的选项2分配您的单元格,然后您可以在ScrollingTableViewCell.h / .m中设置您的单元格。