我有一个带有自定义节标题的UITableViewController,因为我将自己的numberOfSectionsInTableView:
设置为return [self.uploads count];
,而我的numberOfRowsInSection:
设置为return 1;
,我在运行应用程序时从viewForHeaderInSection:
设置中正确获取了所有内容,但是当我运行应用,因为indexPath仅获得1值(由于cellForRowAtIndexPath:
返回1)。
在标题中,我正在更改正确的信息,但是每个部分的单元格都相同:
例如
(第1节)用户名:John
(单元格1)姓氏:史密斯
(第2节)用户名:Maria
(单元格1)姓氏:史密斯
(第3节)用户名:Michael
(单元格1)姓氏:史密斯
如果将numberOfRowsInSection:
的值设置为numberOfRowsInSection
,则会得到以下输出:
例如
(第1节)用户名:John
(单元格1)姓氏:史密斯
(第2单元)姓氏:Williams
(第3单元)姓氏:Peters
(第2节)用户名:Maria
(单元格1)姓氏:史密斯
(第2单元)姓氏:Williams
(第3单元)姓氏:Peters
(第3节)用户名:Michael
(单元格1)姓氏:史密斯
(第2单元)姓氏:Williams
(第3单元)姓氏:Peters
我想要的实际输出是:
(第1节)用户名:John
(单元格1)姓氏:史密斯
(第2节)用户名:Maria
(第2单元)姓氏:Williams
(第3节)用户名:Michael
(第3单元)姓氏:Peters
有什么帮助吗?
编辑:
这是我的代码:
-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
static NSString *CellIdentifier = @"SectionHeader";
UITableViewCell *headerView = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (headerView == nil){
[NSException raise:@"headerView == nil.." format:@"No cells with matching CellIdentifier loaded from your storyboard"];
}
PFObject *audio = [self.uploads objectAtIndex:section];
[self setBlurView:[AMBlurView new]];
[[self blurView] setFrame:CGRectMake(0.f, 0.f, [headerView bounds].size.width, [headerView bounds].size.height)];
[[self blurView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
[self.blurView setBlurTintColor:nil];
[headerView addSubview:[self blurView]];
self.blurView.layer.zPosition = -1;
// Profile pic
NSString *objectId = [audio objectForKey:@"uploader"];
PFUser *user = [PFQuery getUserObjectWithId:objectId];
PFFile *avatar = [user objectForKey:@"avatar"];
[avatar getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
if (!error) {
UIImage *image = [UIImage imageWithData:imageData];
UIImageView *profileImage = (UIImageView *)[headerView viewWithTag:200];
profileImage.image = image;
CALayer * l = [profileImage layer];
[l setMasksToBounds:YES];
[l setCornerRadius:13.0];
}
}];
// Name
UILabel *name = (UILabel *)[headerView viewWithTag:201];
name.text = [audio objectForKey:@"uploaderName"];
// timestamp
NSDate *created = [audio createdAt];
self.timeIntervalFormatter = [[TTTTimeIntervalFormatter alloc] init];
UILabel *timestampLabel = (UILabel *)[headerView viewWithTag:202];
NSTimeInterval timeInterval = [created timeIntervalSinceNow];
NSString *timestamp = [self.timeIntervalFormatter stringForTimeInterval:timeInterval];
[timestampLabel setText:timestamp];
return headerView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [self.uploads count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
PFObject *audio = [self.uploads objectAtIndex:indexPath.row];
UILabel *caption = (UILabel *)[cell viewWithTag:207];
caption.text = [audio objectForKey:@"caption"];
return cell;
}
最佳答案
尝试像这样在cellForRowAtIndexPath:
中设置lastNameLb:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
PFObject *audio = [self.uploads objectAtIndex:indexPath.section]; //indexPath.section
UILabel *caption = (UILabel *)[cell viewWithTag:207];
caption.text = [audio objectForKey:@"caption"];
return cell;
}
请注意,index是
indexPath.section
,而不是indexPath.row
。
关于ios - TableViewController部分和行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22342961/