繁体   English   中英

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

[英]Adding horizontalScroll as subview to UItableviewcell

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

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

鉴于负载

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];

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

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

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

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

如果要在每个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];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM