在UITableViewCell内创建状态栏

时间:2014-12-14 20:32:01

标签: ios objective-c uitableview

我正在尝试根据喜欢和不喜欢的数量创建两个状态栏。我想调整每个单元格的状态栏的大小。我很难调整酒吧的大小。关于如何调整这些条形图,我有两个想法。

  1. 我创建了UIViews并将它们涂成红色和绿色。我想调整它们的大小,但我不确定如何做到这一点,因为Xcode不允许我访问大小参数。

    cell.PositiveBar.layer.frame = CGRectMake(0, 0, 100, 100);
    
  2. enter image description here

    如何让这些状态栏根据喜欢和不喜欢而改变,寻找方法来实现这一目标。

1 个答案:

答案 0 :(得分:0)

如果您有自定义单元格类,则需要添加变量并将它们与视图连接。 在cellForRow方法中,您需要访问Cell和config视图的宽度,因为您可以使用图层或视图,例如更改单元格的状态栏宽度:

- (void)setSellStatusBarByLikes:(NSInteger)likes
{
    CGRect newFrame = self.positiveBar.frame;

    // here you calculate width from likes count with your formula
    newFrame.size.width = likes * dt;
    [self.positiveBar setFrame:newFrame];
}

对于动画,你需要包装你的setFrame方法

[UIView animateWithDuration:.1f animations:^{
  [self.positiveBar setFrame:newFrame];
}];

在此之后,您可以从cellForRowClass

调用此方法
// In table view class
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"HomeCell";
    CustomTableViewCell *cell = [_p_leftTable dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    [cell setSellStatusBarByLikes:10];


    return cell;
}

<强> 修改 因此,您可以在故事板中执行自定义类,或使用以编程方式添加的状态栏来升级UITAbleViewCell类。所有后续步骤取决于您的方法。 当您将CustomTableViewCell中的变量声明为IBoutlet(如果通过storyboard执行)或简单的@property时,它已经是CustomTableViewCell的一部分,那么您可以将它用作self.something。

如果您在默认单元格中以编程方式进行此操作,则只需在cellForRow方法中重新创建状态栏视图。

相关问题