例如,我正在尝试将地名加载到名为UITableView
的顶部myTableView
。在底部,我试图只显示 “test2”。
当然,所有数据都被加载到每个UITableView
中。如果我滚动到myTableView2
的底部,会显示“test2”这个词,但我希望它是唯一存在的。
有没有办法,也许通过委派不同的数据源,我可以将每个UITableView
中的数据分开?我尝试用if语句分离,但很快意识到这没有效果。
在我看来,我可能需要为myTableView2
设置单独的数据源,但我不知道该怎么做......
代码在这里:
- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
[self setString];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
NSString *string1 = [dic objectForKey:@"name"];
for (NSDictionary *diction in dic)
{
[array addObject:string1];
}
[[self myTableView] reloadData];
NSString *string2 = [NSString stringWithFormat:@"test2"];
[array addObject:string2];
[[self myTableView2] reloadData];
[indicator stopAnimating];
indicator.hidden=YES;
[indicator removeFromSuperview];
ilabel.hidden = YES;
}
答案 0 :(得分:0)
解决了以下代码。关键是要有两个不同的数组来加载相应的数据,并在最底部显示它,我可以区分UITableViews与IF语句:
- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
[self setString];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
NSString *string1 = [dic objectForKey:@"name"];
NSString *string2 = [NSString stringWithFormat:@"test2"];
for (NSDictionary *diction in dic)
{
[array addObject:string1];
[array2 addObject:string2];
[[self myTableView] reloadData];
[[self myTableView2] reloadData];
}
[indicator stopAnimating];
indicator.hidden=YES;
[indicator removeFromSuperview];
ilabel.hidden = YES;
}
-(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 == self.myTableView)
{
if ([[array objectAtIndex:indexPath.row] isKindOfClass:[NSNumber class]])
{
cell.textLabel.text = [[array objectAtIndex:indexPath.row] stringValue];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
}
else
{
cell.textLabel.text = [array objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
}
}
if (tableView == self.myTableView2)
{
if ([[array2 objectAtIndex:indexPath.row] isKindOfClass:[NSNumber class]])
{
cell.textLabel.text = [[array2 objectAtIndex:indexPath.row] stringValue];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
}
else
{
cell.textLabel.text = [array2 objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
}
}
return cell;
}