[英]Working with TableViews and CollectionViews and managing multiple data sources in Swift
[英]Multiple TableViews in one View - Array of data sources?
我正在尝试重新创建类似于iPhone上附带的“天气”应用程序的视图控制器。 我有一个滚动视图,其中每页包含一个单独的表格视图,并且我正在努力寻找一种为每个表格视图指定数据的方法。
棘手的是,每个表视图中的信息以及表视图本身的更改都基于用户默认值。 基本上,我有一个存储在用户默认值中的字典数组,并且该数组中的每个对象都有其自己的表视图。 每个字典都包含一个标题,一个经纬度和一个长经。 创建表时,我还使用lat和long通过api调用和解析器从Internet上获取一些数据。 这是代码:
- (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];
}
不幸的是,这一直没有为我正确地工作。 是否有人对如何完成这项工作有任何想法?
谢谢!!
当然
if (tableView.tag = i) {
应该
if (tableView.tag == i) {
即便如此,这也是一个奇怪的构想-不确定您要达到的目标。 您将i设置为标签,然后检查标签是否为i? 真的没有道理-那是我建议您仔细看看并重新考虑逻辑的地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.