单个ViewController中的多个UITableview
我有一个viewcontroller
,我想显示3个表格(因为内容和表格属性是不同的)。 如何在一个viewcontroller
为这3个表添加这些委托方法?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [array1 count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {}
编辑
那么,如果我想添加一个uislider
到一个表格行使用自定义cell
,当我滑动的值,我想改变显示亮度,我该怎么办?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { if (tableView == _displayThemes) { return 1; } else { return 1; } } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(tableView==_displayThemes) { return 1; } else { return 5; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } if (tableView == _displayThemes) { cell.textLabel.text = [displaytheme objectAtIndex:indexPath.row]; return cell; } else { cell.textLabel.text = [fontlist objectAtIndex:indexPath.row]; return cell; } } - (IBAction)fontButton:(id)sender { _fontList = [[UITableView alloc]init]; [self.view addSubview:_fontList]; [UIView animateWithDuration:1.5 delay:0 options: UIViewAnimationOptionTransitionCurlUp animations:^{ _fontList.fram e= CGRectMake(0,800,320,200); } completion:^(BOOL finished){ _fontList.frame = CGRectMake(0,280,320,200); }]; [_fontList reloadData]; } - (IBAction)button:(id)sender { _displayThemes = [[UITableView alloc]init]; [self.view addSubview:_displayThemes]; [UIView animateWithDuration:1.5 delay:0 options: UIViewAnimationOptionTransitionCurlUp animations:^{ _displayThemes.frame=CGRectMake(0,800,320,200); } completion:^(BOOL finished){ _displayThemes.frame=CGRectMake(0,280,320,200); }]; }
这将与您在一个表视图中执行的操作相同,但您应该检查当前正在使用哪个tableview。
myTableView1.dataSource = self; ... - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView == myTableView1) { // your code 1 } else if (tableView == myTableView2) { // your code 2 } else if (tableView == myTableView3) { // your code 3 } }
编辑:
关于亮度:
如何改变iOS 5应用程序的亮度?
而关于UISlider
它具有minimunValue
和maximumValue
属性。
- (void) sliderChanged:(UISlider*)sender{ UISlider *slider = (UISlider*)sender; [[UIScreen mainScreen] setBrightness:slider.value]; }
编辑:
slider.tag = 1; [cell addSubview:slider]; ... // when you need.. indexPath = [NSIndexPath indexPathForRow:myRow inSection:mySecion]; UISlider* slider = (UISlider*) [[self.tableView cellForRowAtIndexPath:indexPath] viewWithTag:1];
你总是得到一个引用,并且总是可以检查调用哪个tableView委托或dataSource方法。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { if (tableView == self.tableView1) { return 1; } if (tableView == self.tableView2) { return 1; } if (tableView == self.tableView3) { return 1; } }
所有表格使用相同的标识符都不会获得任何收益。 使用类似于:
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { if (tableView == self.tableView1) { static NSString *CellIdentifier1 = @"cellForTable1"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1]; } cell.textLabel.text = [NSString stringWithFormat: @"table1: %d.%d", indexPath.section, indexPath.row]; return cell; } if (tableView == self.tableView2) { static NSString *CellIdentifier2 = @"cellForTable2"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2]; } cell.textLabel.text = [NSString stringWithFormat: @"table2: %d.%d", indexPath.section, indexPath.row]; return cell; } if (tableView == self.tableView1) { static NSString *CellIdentifier3 = @"cellForTable3"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier3]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier3]; } cell.textLabel.text = [NSString stringWithFormat: @"table3: %d.%d", indexPath.section, indexPath.row]; return cell; } }
//add tag in tableView . myTable1.tag = 200; myTable2.tag = 201; myTable3.tag = 202; - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { if (tableView.tag == 200) { return 1; } if (tableView.tag == 201) { return 1; } if (tableView.tag == 202) { return 1; } }
以前没有为我工作,我拿出以下解决scheme:
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if((tableView1 != nil) && (tableView == tableView1)) { return Items1.count } else if((tableView2 != nil) && (tableView == tableView2)) { return Items2.count } else if((tableView3 != nil) && (tableView == tableView3)) { return Items3.count } else { return 0 } }
- 将二进制file upload到iTunes时出现的问题在高Sierra上使用Xcode 9进行连接(ERROR ITMS-90391)
- 我编译Swift 2.3项目,并在Xcode 8.1上有这个问题
- iOS项目显示i386链接器错误
- 如何实现使用Game Center的最佳分数今日排行榜?
- 多个video,同一屏幕
- 如何在iphone中获取通话logging?
- 在UIWebView上使用swift 2进行POST不起作用
- 以编程方式隐藏苹果iAd ADBannerView
- 当我使用UIImagePNGRepresentation或UIImageJPEGRepresentation将UIImage转换为NSdata时,图像大小增加太多