2012-09-25 121 views
1

基本上,我有一个表视图与2个按钮被呈现到前两行,我已经通过CGRectMake的完成并将按钮添加到单元格子视图。表格视图还有一个带有范围栏的搜索栏。自定义UITableViewCell与按钮

我遇到的问题是,当应用第一次加载时,按钮比我想要的要短,并且设置为(几乎就好像它们在呈现时是“默认的”)。当我改变范围时,按钮会变成我设定的大小,但当我换回时,按钮不会恢复到原来的大小。

按钮正在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

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

另一个问题是,在创建单元格时,您正在从互联网上同步加载内容。如果没有其他性能方面的原因,您会希望使用NSURLConnection之类的方法异步执行此操作。使用占位符图像作为按钮,并在互联网上加载实际图像时替换这些图像。或者,您似乎只有两张图像,所以至少您可以缓存它们,而不是每次创建单元时都加载它们。

+0

感谢您的帮助,我现在只是加载我的Mac,所以我会给它一个去。 'topRowCell'中有两行是按钮的功能,我将移动if语句中的所有按钮创建行。看看我从哪里去。 –

+0

将按钮创建行移动到“if(topRowCell == nil)”语句中并不起作用,实际上它将按钮从完全呈现中移除。 –

+0

你可以用你现在的代码更新你的文章吗?然后我们都可以再看一眼。 – onnoweb