【问题标题】:TableViewController sections and rowsTableViewController 部分和行
【发布时间】:2014-03-12 05:45:04
【问题描述】:

我有一个带有自定义节标题的 UITableViewController,因为我将我的 numberOfSectionsInTableView: 设置为 return [self.uploads count]; 并将我的 numberOfRowsInSection: 设置为 return 1; 我在运行应用程序时从我的 viewForHeaderInSection: 正确设置中获取所有内容,但是当我运行应用程序时,cellForRowAtIndexPath: 总是得到相同的值,因为 indexPath 只得到 1 个值(由于 numberOfRowsInSection: 返回 1)。

在我的标题中,我正在更改正确的信息,但每个部分的单元格都是相同的:

例如。
(第 1 部分)用户名:John
(单元格 1)姓:史密斯

(第 2 节)用户名:Maria
(单元格 1)姓:史密斯

(第 3 部分)用户名:Michael
(单元格 1)姓:史密斯

如果我将numberOfRowsInSection 的值设置为return [self.uploads count],我会得到以下输出:

例如。
(第 1 部分)用户名:John
(单元格 1)姓:史密斯
(单元格 2)姓:威廉姆斯
(单元格 3)姓:彼得斯

(第 2 节)用户名:Maria
(单元格 1)姓:史密斯
(单元格 2)姓:威廉姆斯
(单元格 3)姓:彼得斯

(第 3 部分)用户名:Michael
(单元格 1)姓:史密斯
(单元格 2)姓:威廉姆斯
(单元格 3)姓:彼得斯

我想要的真正输出是:
(第 1 部分)用户名:John
(单元格 1)姓:史密斯

(第 2 节)用户名:Maria
(单元格 2)姓:威廉姆斯

(第 3 部分)用户名:Michael
(单元格 3)姓:彼得斯

有什么帮助吗?

编辑:

这是我的代码:

-(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;
}

【问题讨论】:

  • 你能展示你的代码吗?
  • 我添加了代码,感谢您的快速响应!
  • 如果这些名称来自数组,您可能必须使用 NSIndexPath -section,作为名称数组中名称的索引(或 self.uploads,如果是这样的话)对吗?
  • 就是这样!谢谢你riadhluke!

标签: ios objective-c uitableview


【解决方案1】:

尝试像这样在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;
}

请注意,索引是indexPath.section,而不是indexPath.row

【讨论】:

  • 做到了!非常感谢!
【解决方案2】:

在 cellForRowAtIndexPath: 你只检查 indexPath.row 。 indexPath 有两个值要检查:indexPath.section 和 indexPath.row。检查部分属性的值以选择正确的“姓氏”属性。

【讨论】:

  • 对不起,我对使用 UITableViewCells 很陌生,检查 indexPath.section 的代码是什么?它是否进入 cellForRowAtIndexPath:?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-16
相关资源
最近更新 更多