将horizo​​ntalScroll作为子视图添加到UItableviewcell

时间:2013-11-29 05:19:09

标签: ios objective-c uitableview

我有一个tableview,每个单元格有八个按钮。我需要水平滚动才能滚动这些按钮。

使用此代码时,我无法在单元格中看到任何按钮。

  

在视图中加载

horizontalScroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 270, 320, 60)];
//horizontal scroll is a property of UIViewController

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
         static NSString *CellIdentifier = @"Cell";
         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

         // Configure the cell...
         if (cell == nil) 
         {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
          }

int x = 13;
int y=13;

for (int i = 0; i < 8; i++) {
    UIButton *button= [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(x, 80, 45, 22);
    [button setTag:i];
    [button setTitle:@"08:30" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor colorWithRed:234.0/255 green:234.0/255 blue:234.0/255 alpha:1]];
    button.titleLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:13.0];
    [horizontalScroll addSubview:button];
    x +=62;

  }

  [cell addSubview:horizontalScroll];
}

但是,如果我这样做:

   [cell addSubview:button];

我可以在单元格中看到按钮,但无法滚动它们。

任何帮助都将不胜感激!!

3 个答案:

答案 0 :(得分:0)

添加[horizontalScroll setContentSize:CGSizeMake(height, WidthofyourContents)];

答案 1 :(得分:0)

Scroller.contentSize = CGSizeMake([imgArray count] * 75,60); 否则,请尝试使用圆角矩形按钮进行测试,这有助于识别问题。

答案 2 :(得分:0)

如果要在每个tableview单元格中添加水平滚动视图, 那么你必须在tableview单元格中添加水平滚动视图,这意味着在“cellForRowAtIndexPath”方法中添加水平滚动视图。

//

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
         static NSString *CellIdentifier = @"Cell";
         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

         // Configure the cell...
         if (cell == nil) 
         {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
          }

int x = 13;
int y=13;
UIScrollView *horizontalScroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];
[horizontalScroll setContentSize:(CGSizeMake(790, 60))];
for (int i = 0; i < 8; i++) {
    UIButton *button= [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(x, 0, 45, 22);
    [button setTag:i];
    [button setTitle:@"08:30" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor colorWithRed:234.0/255 green:234.0/255 blue:234.0/255 alpha:1]];
    button.titleLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:13.0];
    [horizontalScroll addSubview:button];
    x +=62;

  }
  [cell addSubview:horizontalScroll];
}