基本上,我有一个表格视图,其中有 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;
}
}
让我知道您是否还有其他想要的。