gpt4 book ai didi

iphone - 在每个单元格中包含 UI 元素的 UITableView 中缓慢滚动

转载 作者:行者123 更新时间:2023-11-29 11:20:06 25 4
gpt4 key购买 nike

我在 iPad 上的表格 View 的每个单元格中有 3 个分段控件。应用程序的方向始终是横向的,并且每次在应用程序上运行时单元格的数量都不同。如果行数大约小于 10,则该应用程序运行良好,但在任何高于该行数的地方,就会开始出现故障。

对于我正在构建的应用程序类型,我可能有多达 70 行 ==> 意思是,210 个 UISegmentedControl,一次全部分配到内存中。

有解决办法吗?有什么方法可以重用这些 UISegmentedControls 吗?如果是,如何保留分段控件的状态?

否则,有人可以提出新的解决方案吗? (每个分段控件都有项目“A”和“B”,并且有三个分段控件代表对应于表的每一行的每个对象的三个不同参数)。

更新:

代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell...
segmentedControl1 = (UISegmentedControl*)[array1 objectAtIndex:[indexPath row]];
segmentedControl1.frame = CGRectMake(180, 15, 100, 30);
[cell.contentView addSubview:segmentedControl1];

segmentedControl2 = (UISegmentedControl*)[array2 objectAtIndex:[indexPath row]];
segmentedControl2.frame = CGRectMake(450, 15, 100, 30);
[cell.contentView addSubview:segmentedControl2];

segmentedControl3 = (UISegmentedControl*)[array3 objectAtIndex:[indexPath row]];
segmentedControl3.frame = CGRectMake(725, 15, 100, 30);
[cell.contentView addSubview:segmentedControl3];

cell.selectionStyle = UITableViewCellSelectionStyleNone;

return cell;
}

最佳答案

创建自定义 UITableViewCell 子类。在其中为您的三个 UISegmentedControl 添加三个属性,并将它们添加到 init 方法中:

@interfate MyCell : UITableViewCell
@property (nonatomic, retain, readonly) UISegmentedControl *control1;
@property (nonatomic, retain, readonly) UISegmentedControl *control2;
@property (nonatomic, retain, readonly) UISegmentedControl *control3;
@end

@implementation

@synthesize control1 = _control1;
@synthesize control2 = _control2;
@synthesize control3 = _control2;

- (id)init
{
if ((self = [super init]))
{
_control1 = [[UISegmentedControl alloc] init...];
_control1.frame = CGRectMake(...);
[self addSubView:_control1];

// repeat with control2 & control3
}
//...
@end

然后,您可以让 NSNumber 数组保存选定的索引,而不是存储 UISegmentedControl 数组。

然后你会做类似的事情:

cell.control1.selectedIndex = [[array1 objectAtIndex:indexPath.row] integerValue];

您还可以创建自定义对象来保存这些数据并将它们存储在一个数组中。

关于iphone - 在每个单元格中包含 UI 元素的 UITableView 中缓慢滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7682213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com