以编程方式将故事板UITableView和自定义UITableViewCells添加到UIScrollView

时间:2015-06-18 01:42:55

标签: uitableview uiscrollview storyboard

在我的Storboard中,我有一个UIViewController(ParentClass) - > UIScrollView(链接到Parent) - > UITableView(链接到Parent) - > UITableViewCell(2个单元格,每个单元格都有自己的类)。

我要做的是将UITableView的几个实例(带有自定义单元格)添加到UIScrollView中。

我接近以下代码,但是单元格是空的:

self._scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self._scrollView.pagingEnabled = YES;
self._scrollView.showsVerticalScrollIndicator = NO;
self._scrollView.alwaysBounceVertical = NO;

NSInteger numberOfViews = 22;
for (int i = 0; i < numberOfViews; i++) {
    CGFloat xOrigin = i * self.view.frame.size.width;

    UITableViewController *theTableView = [[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped];
    theTableView.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    theTableView.tableView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self._scrollView.frame.size.height);
    theTableView.tableView.backgroundColor = [UIColor colorWithRed:246/255.0 green:248/255.0 blue:249/255.0 alpha:1];
    theTableView.tableView.delegate = self;
    theTableView.tableView.dataSource = self;
    theTableView.tableView.tag = 100+i;
    [self._scrollView addSubview:theTableView.tableView];
}
self._scrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);

[self.view addSubview:self._scrollView];

显示所有表格,所有页面都在那里,滚动应该如此,但自定义单元格布局没有显示。

这是我用来布局表格的代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([indexPath row] == 0) {
        return 127.0;
    } else {
        return 56.0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        static NSString *CellIdentifier = @"MealImageCell";
        MealImageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[MealImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        [self configureImageCell:cell atIndexPath:indexPath];

        return cell;
    } else {
        static NSString *CellIdentifier = @"MealInfoCell";
        MealInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[MealInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        [self configureInfoCell:cell atIndexPath:indexPath];

        return cell;
    }
}

- (void)configureImageCell:(MealImageCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    NSMutableDictionary *dictMeal = [[NSMutableDictionary alloc] init];
    if ([arrayMeals count]) {
        dictMeal = [arrayMeals objectAtIndex:indexPath.section];
        cell.userInteractionEnabled = YES;
    } else {
        cell.userInteractionEnabled = NO;
    }

    [cell.imageFood sd_setImageWithURL:[NSURL URLWithString:[dictMeal valueForKey:@"img_src"]]
                      placeholderImage:[UIImage imageNamed:[NSString stringWithFormat:@"meal%li.jpeg",(long) indexPath.section+1]]];
}

- (void)configureInfoCell:(MealInfoCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    NSString *mealName;
    if ([arrayMeals count]) {
        NSMutableDictionary *dictMeal = [arrayMeals objectAtIndex:indexPath.section];

        cell.userInteractionEnabled = YES;

        if ([dictMeal valueForKey:@"mealDescription"] == [NSNull null]) {
            mealName = NSLocalizedString(@"No Name", nil);
        } else if ([[dictMeal valueForKey:@"mealDescription"] isEqualToString:@""]) {
            mealName = NSLocalizedString(@"Deleted", nil);
        } else {
            mealName = [decodeHTMLEntities decodeHTMLEntities:[[dictMeal valueForKey:@"mealDescription"] stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
        }
    } else {
        cell.userInteractionEnabled = NO;
        mealName = NSLocalizedString(@"Loaing Meal...", nil);
    }

    cell.labelFood.text = mealName;
}

如果没有scrollView,表格将按原样填充。

1 个答案:

答案 0 :(得分:0)

答案是重新说明:

MealImageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

MealImageCell *cell = [_myTablView dequeueReusableCellWithIdentifier:CellIdentifier];

_myTableView是Storyboard中表视图的IBOutlet。