简体   繁体   中英

ios custom Uitableview cell with xib

I want to display N number of button on single cell of tableview. for that i have created xib of one button and i want to load that xib N number of times into my tableview cell.

I use following code but it just display xib one time in every row. I have used for loop and scrollview to have multiple view of xib one single row but it only display one time.

Update :- I change my table vie code as suggested in answer. but my scrollview is not working horizontally and i got extra xib view at starting of every row. I think this is beacuse of [self.table registerNib:[UINib nibWithNibName:@"wagon" bundle:nil] forCellReuseIdentifier:@"Cell"]; in view did load..how can i solve this problem?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {

 static NSString *CellIdentifier = @"Cell";



  UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];


  if (cell==nil) {

    }


  cellScrollViewClass  * scrollView  = [[cellScrollViewClass alloc]   initWithFrame:CGRectMake(MyPadding,14,myWidth-MyPadding*2,200)]; // Scrollview
  scrollView.scrollEnabled=YES;
  scrollView.userInteractionEnabled=YES;
  scrollView.showsHorizontalScrollIndicator=NO;
  scrollView.contentSize = CGSizeMake((336*NumberofCell),187); // To make ContentSize for item to scroll
for (int i = 0; i < NumberofCell; i++)
{ // for loop to add 2 wagon view and 1 engine view on Left hand side Custom cell.

    UIView *nib = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    UIView *wagonView = [[UIView alloc]initWithFrame:CGRectMake((385*i)-200, 18, 385, 163)];
    [wagonView addSubview:nib];
    [scrollView addSubview:wagonView];
}
cell.accessoryView=scrollView;

  return cell;
   }

In view did load i register nib

[[self table]setDelegate:self];
[[self table]setDataSource:self];
[self.view addSubview:table];
[table setShowsHorizontalScrollIndicator:NO];
[table setShowsVerticalScrollIndicator:NO];
table.separatorColor = [UIColor clearColor];
[table setContentInset:UIEdgeInsetsMake(-15,0,0,0)];
[self.table registerNib:[UINib nibWithNibName:@"wagon" bundle:nil] forCellReuseIdentifier:@"Cell"];

move your code outside if (cell==nil){} . This condition will return YES only if it fails to dequeue.

创建一个(主)UIView,将所有wagonView添加到其中,然后在循环后将“ Main”视图添加到Scrollview。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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