带按钮的自定义UITableViewCell

时间:2012-09-25 19:00:34

标签: iphone objective-c uitableview uibutton cgrectmake

基本上,我有一个表格视图,其中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;
    }
}

如果您还有其他任何需要,请告诉我。

1 个答案:

答案 0 :(得分:1)

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

另一个问题是,您在创建单元格时会同步从Internet上加载内容。除了出于性能原因,你还想要使用像NSURLConnection这样的东西进行异步操作。使用占位符图像作为按钮,并在从互联网上加载真实图像时替换这些图像。或者,您似乎只有两个图像,所以至少您可以只缓存它们,而不是每次创建单元格时都加载它们。

相关问题