0
我试图重新创建一个视图控制器,类似于iPhone上的股票“天气”应用程序。我有一个滚动视图,每页包含一个单独的tableview,并且我正在努力想出一种方法来为每个表视图指定数据。一个视图中有多个TableViews - 数据源数组?
棘手的部分是,每个tableview中的信息和tableviews本身都会根据用户的默认值进行更改。基本上,我有一个存储在用户默认值中的字典数组,并且此数组中的每个对象都有自己的tableview。每个字典都包含一个标题,一个经纬度和一个长度。当我创建表格时,我还使用lat和long从api调用和解析器获取来自互联网的一些数据。这里的代码:
- (void)setupScrollView
{
scrollView.delegate = self;
[self.scrollView setBackgroundColor:[UIColor clearColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
//this is an array of dictionaries that hold a location title, as well as a lat and lng.
NSArray *arrayForLocations = [NSArray arrayWithArray:[appDelegate.defaults objectForKey:@"arrayOfLocationDicts"]];
NSLog(@"Array of Location Dicts holds: %@",arrayForLocations);
for (int i = 0; i < arrayForLocations.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
[self.scrollView addSubview:subview];
UITableView *tbView = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, scrollView.frame.size.width, scrollView.frame.size.height - 40)];
tbView.backgroundColor = [UIColor grayColor];
tbView.tag = i;
tbView.delegate = self;
tbView.dataSource = self;
[subview addSubview:tbView];
UILabel *locationLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 260, 40)];
locationLabel.textColor = [UIColor blackColor];
locationLabel.text = [[arrayForLocations objectAtIndex:i]objectForKey:@"location_address"];
[subview addSubview:locationLabel];
pageControl.numberOfPages = [arrayForLocations count];
//get coordinate from dictionary
CLLocationCoordinate2D eventCoordinate;
eventCoordinate.latitude = [[[arrayForLocations objectAtIndex:i]objectForKey:@"location_latitude"]floatValue];
eventCoordinate.longitude = [[[arrayForLocations objectAtIndex:i]objectForKey:@"location_longitude"]floatValue];
//turn coordinate into data
SDJConnection *connection = [[SDJConnection alloc]init];
NSMutableArray *singleDataSource = [connection getEventInfoWithCoordinate:eventCoordinate];
//store data in array
[arrayOfDataSources addObject:singleDataSource];
NSLog(@"array of Data Sources in the scrollsetup: %@",arrayOfDataSources);
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * arrayForLocations.count, self.scrollView.frame.size.height);
}
所以现在我的问题是告诉每个这些表什么数据显示。我首先想到的是设置表的标签,正如我上面做的,然后有这样的事情在的cellForRowAtIndexPath -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
int i = tableView.tag;
if (tableView.tag = i) {
cell.textLabel.text = [[[arrayOfDataSources objectAtIndex:i]objectAtIndex:indexPath.row]eventTitle];
}
,不幸的是还没有被正确地为我工作。有没有人有任何想法如何做到这一点?
谢谢!
我基本上有一个数组数组,每个数组都包含我想要的信息在相应的表中。我试图做的是将表与正确的数据数组进行匹配 - 换句话说,表0从索引0处的数据数组获取数据,表1从索引1处的数据数组获取数据,依此类推。有没有更好的方法来解决这个问题? – 2012-04-20 22:27:07
我的意思是,这个特定的条件比较有什么意义?究竟什么是不正确的,你能描述发生了什么吗? – 2012-04-21 02:06:44