gpt4 book ai didi

ios - TableView 单元格无法加载两个 TableView

转载 作者:行者123 更新时间:2023-11-29 04:42:07 27 4
gpt4 key购买 nike

如果我只加载一个 TableView ,我的 View 中必须有两个 TableView ,它工作正常。但是当我尝试使用下面的方法加载两个表格 View 时,它给出了以下异常。

未捕获异常“NSRangeException”,原因:“* -[NSArray objectAtIndex:]:索引 2 超出范围 [0 .. 1]”

- (void)viewDidLoad {
[super viewDidLoad];

array1 = [[NSArray alloc]initWithObjects:@"Start",@"End",@"Frequency",@"Time of Day",nil];
array2 =[[NSArray alloc]initWithObjects:@"Alarm",@"Tone",nil];

table1.scrollEnabled =NO;
table2.scrollEnabled =NO;

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (tableView == table1) ;
return 1;

if (tableView == table2);
return 1;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.table1) ;
return [array1 count];
if (tableView == self.table2) ;
return [array2 count];

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell...


if (tableView == self.table1){
cell.textLabel.text = [array1 objectAtIndex:indexPath.row];

}
if (tableView == self.table2){
cell.textLabel.text = [array2 objectAtIndex:indexPath.row];

}
return cell;}

最佳答案

您可能请求索引处的对象大于数组中的项目之一。您是否正确实现了 – numberOfSectionsInTableView:– tableView:numberOfRowsInSection: 方法来检查它们调用的是哪个表并根据您的数据数组返回适当的值?
更新
按如下方式编辑方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
if (tableView == self.table1)
return 1;

if (tableView == self.table2)
return 1;

return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.table1)
return [array1 count];
if (tableView == self.table2)
return [array2 count];

return 0;
}

关于ios - TableView 单元格无法加载两个 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10243487/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com