【问题标题】:Custom UITableViewCell with buttons带有按钮的自定义 UITableViewCell
【发布时间】:2012-09-25 19:00:33
【问题描述】:

基本上,我有一个表格视图,其中有 2 个按钮被渲染到前两行,我通过 CGRectMake 完成了这项工作,并将按钮添加到单元格子视图中。表格视图还有一个带有范围栏的搜索栏。

我遇到的问题是,当应用程序首次加载时,按钮比我希望的要短,并且已将它们设置为(几乎就像它们在呈现时被“默认”一样)。当我更改范围时,按钮会更改为我设置的大小,但是当我更改回来时,按钮不会恢复到之前的大小。

按钮是在cellForRowAtIndexPath 方法中创建的,虽然我看不出这可能是大小的问题,但我确实对范围栏有响应问题(但我知道这很可能是关闭的每次范围更改时都会调用cellForRowAtIndexPath 中的所有内容,因此它自然会导致应用程序运行缓慢。

cellForRowAtIndexPath 方法在这里:

-- Edited 26/09/2012 After First Answer --

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *normalCellIdentifier = @"normalCell";
    static NSString *topRowCellIdentifier = @"topRowCell";

    UITableViewCell *normalCell = [tableView dequeueReusableCellWithIdentifier:normalCellIdentifier];
    if (normalCell == nil) {
        normalCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:normalCellIdentifier];
    }

    UITableViewCell *topRowCell = [tableView dequeueReusableCellWithIdentifier:topRowCellIdentifier];
    if (topRowCell == nil) {
        topRowCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:topRowCellIdentifier];

        // Added in here
        button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        CGRect buttonRect;

        button1.tag = indexPath.row;
        button2.tag = indexPath.row;

        button1.titleLabel.tag = 0;
        button2.titleLabel.tag = 1;

        [button1 setTitle:@"Button 1" forState:UIControlStateNormal];
        [button2 setTitle:@"Button 2" forState:UIControlStateNormal];

        [button1 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [button2 addTarget:self action:@selector(buttonPressed2:) forControlEvents:UIControlEventTouchUpInside];

        [topRowCell addSubview:button1];
        [topRowCell addSubview:button2];

        buttonRect = CGRectMake(0.0f, 0.0f, topRowCell.frame.size.width / 2.0f, topRowCell.frame.size.height);

        button1.frame = CGRectMake(7.0f, 7.0f, buttonRect.size.width - 14.0f, buttonRect.size.height- 14.0f);
        button2.frame = CGRectMake(buttonRect.size.width + 7.0f, 7.0f, buttonRect.size.width - 14.0f, buttonRect.size.height - 14.0f);

        NSMutableArray *buttonsArray = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.avicode.co.uk/iphone/minepedia/plists/featured/buttons.plist"]];

        NSData *data1;
        data1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[[buttonsArray objectAtIndex:indexPath.row] objectAtIndex:button1.titleLabel.tag] objectForKey:@"button1ImageURL"]]];

        NSData *data2;
        data2 = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[[buttonsArray objectAtIndex:indexPath.row] objectAtIndex:button2.titleLabel.tag] objectForKey:@"button2ImageURL"]]];

        selectedIndexPathType1 = [[[buttonsArray objectAtIndex:indexPath.row] objectAtIndex:button1.titleLabel.tag] objectForKey:@"type"];
        selectedIndexPathType2 = [[[buttonsArray objectAtIndex:indexPath.row] objectAtIndex:button2.titleLabel.tag] objectForKey:@"type"];

        button1.imageView.image = [UIImage imageWithData:data1];
        button2.imageView.image = [UIImage imageWithData:data2];
    }

    // Configure the cell...
    if (segmentedControl.selectedSegmentIndex == 0) 
    {
        if (indexPath.section == 0) 
        {
            topRowCell.selectionStyle = UITableViewCellSelectionStyleNone;
            return topRowCell;
        }
        else
        {
            return normalCell;
        }
    }
    else
    {
        return normalCell;
    }
}

如果你还想要什么,请告诉我。

【问题讨论】:

    标签: iphone objective-c uitableview uibutton cgrectmake


    【解决方案1】:

    我认为您的部分问题是您要多次创建按钮并将其添加到单元格中。您应该在 if 语句的 then 子句中创建按钮,以测试您是否成功地将先前创建的单元格出队。

    另一个问题是您在创建单元格时从互联网同步加载内容。如果只是出于性能原因,您将希望使用 NSURLConnection 之类的东西异步执行此操作。为您的按钮使用占位符图像,并在您从 Internet 加载真实图像时替换它们。或者,您似乎只有两个图像,因此至少您可以缓存它们而不是每次创建单元格时加载它们。

    【讨论】:

    • 感谢您的帮助,我现在正在加载我的 Mac,所以我会试一试。 topRowCell 中有 2 行是按钮所在的位置,我将在 if 语句中移动所有按钮创建行。看看我从那里去哪里。
    • 将按钮创建行移动到if (topRowCell == nil) 语句中不起作用,实际上它完全删除了按钮的呈现。
    • 你能用你现在拥有的代码更新你的帖子吗?然后我们都可以再看看。
    • 您应该使用 [topRowCell.contentView addSubview;] 而不是 [topRowCell addSubView:]。如果 button1.tag 和 button2.tag 值在 [cellForRowAtIndexPath:] 方法之外很重要,那么您应该将它们的分配移到 if 语句之外。
    • 我发现了问题,这与代码无关,在情节提要中,topRowCell 的高度是 71,而不是应该的 90。但是您能否编辑您的帖子并告诉我如何让我的应用异步执行该操作。
    猜你喜欢
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    • 2018-10-01
    • 1970-01-01
    相关资源
    最近更新 更多