【发布时间】:2023-03-26 09:36:01
【问题描述】:
我有一个自定义 UITableViewCell ,其中包含一个 UISegmentedControl ,如下所示: #导入
@interface TFSegmentedControlCell : UITableViewCell {
UISegmentedControl *segmentedControl;
UILabel *questionTitle;
}
@property (nonatomic, retain) UISegmentedControl *segmentedControl;
@property (nonatomic, retain) UILabel *questionTitle;
@end
我正在重用这些单元以进行良好的内存管理,并且我可以设想我将拥有 100 个左右的单元的情况。段标题取自从在线加载的数组。我这样做如下:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
TFSegmentedControlCell *cell = (TFSegmentedControlCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TFSegmentedControlCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSArray *segmentHeaders = [[[self.questionsArray objectAtIndex:indexPath.row] valueForKey:@"answer_labels"] componentsSeparatedByString:@","];
for (int i=0; i<[segmentHeaders count]; i++) {
[cell.segmentedControl insertSegmentWithTitle:[segmentHeaders objectAtIndex:i] atIndex:cell.segmentedControl.numberOfSegments animated:NO];
}
cell.questionTitle.text = [NSString stringWithFormat:@"%u. %@",indexPath.row + 1,[[self.questionsArray objectAtIndex:indexPath.row] valueForKey:@"description"]];
return cell;
}
问题是,选定的索引与单元格而不是索引保持一致,即,如果我向下滚动并加载了一个新单元格(从屏幕外),它将具有第一个单元格的选定索引(现在在屏幕外) .如果我在新加载的单元格中选择不同的段,当我向上滚动时,它将更改第一个单元格的选定段。
当一个段被选中时,我目前没有调用任何方法,只是在按下“完成”按钮时遍历所有 UISegmentedControls 并将所有 selectedSegments 添加到一个数组并将其发送到在线 php 文件进行处理。
非常感谢您的想法:)
解决方案:
我将int row 添加到TFSegmentedControlCell.h
然后我添加了一个当 UISegmentedControl 值发生变化时的方法:
-(void)selectedTFSegment:(id)sender {
TFSegmentedControlCell *cell = (TFSegmentedControlCell *)[[sender superview] superview];
[[self.questionsArray objectAtIndex:cell.row] setValue:[NSString stringWithFormat:@"%i",cell.segmentedControl.selectedSegmentIndex] forKey:@"selectedSegment"];
}
然后重新配置单元的初始化:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
int row = indexPath.row;
static NSString *CellIdentifier = @"Cell";
TFSegmentedControlCell *cell = (TFSegmentedControlCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TFSegmentedControlCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSArray *segmentHeaders = [[[self.questionsArray objectAtIndex:row] valueForKey:@"answer_labels"] componentsSeparatedByString:@","];
[cell.segmentedControl removeAllSegments];
for (int i=0; i<[segmentHeaders count]; i++) {
[cell.segmentedControl insertSegmentWithTitle:[segmentHeaders objectAtIndex:i] atIndex:cell.segmentedControl.numberOfSegments animated:NO];
}
cell.row = row;
[cell.segmentedControl addTarget:self action:@selector(selectedTFSegment:) forControlEvents:UIControlEventValueChanged];
if ([[self.questionsArray objectAtIndex:row] objectForKey:@"selectedSegment"] != nil) {
int newIndex = [[[self.questionsArray objectAtIndex:row] objectForKey:@"selectedSegment"] intValue];
[cell.segmentedControl setSelectedSegmentIndex:newIndex];
} else {
[cell.segmentedControl setSelectedSegmentIndex:UISegmentedControlNoSegment];
}
cell.questionTitle.text = [NSString stringWithFormat:@"%u. %@",indexPath.row + 1,[[self.questionsArray objectAtIndex:row] valueForKey:@"description"]];
return cell;
}
【问题讨论】:
标签: iphone ios4 uitableview uisegmentedcontrol