基本上,我有一个表格视图,两个按钮被渲染到顶部两行,我已经通过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;
}
}如果你还有什么需要的,请告诉我。
发布于 2012-09-26 05:06:52
我认为您的部分问题是您多次创建按钮并将其添加到单元格中。您应该在if-语句的then-子句中创建按钮,以测试您是否成功地将先前创建的单元出队。
另一个问题是,在创建单元格时,您需要同步加载internet上的内容。如果只是出于性能方面的原因,您将希望使用诸如NSURLConnection之类的东西来异步完成此操作。将占位符图像用于按钮,并在从互联网上加载真实图像时替换这些图像。或者,您似乎只有两个图像,所以至少您可以只缓存它们,而不是每次创建单元时都加载它们。
https://stackoverflow.com/questions/12589413
复制相似问题