我有UITableViewController
,其中包含TableView
和UILabel
。 UITableViewSource
通过自定义UITableViewCell将数据提供给TableView
。 UITableViewCell
由一个可以添加项目的Stepper和Label保存该值组成。
当用户点击Stepper时,我想更新UILabel
中的UIViewController
,而我似乎无法让它工作......
非常感谢任何帮助/提示!这是我的测试项目中的示例代码,有一堆未使用的代码,但这只是一个测试,所以我让它在那里。
谢谢,
守则
我的自定义单元格类是" StepperDetailsCell"。
@interface StepperDetailsCell : UITableViewCell
@property (nonatomic,weak) id <StepperDetailsCellDelegate> delegate;
@property (strong, nonatomic) IBOutlet UIImageView *imagegroceries;
@property (strong, nonatomic) IBOutlet UIStepper *objstepper;
@property (strong,nonatomic) IBOutlet UILabel *lblsteppervalue;
-(IBAction)stepperclicked:(UIStepper*)sender;
@end
和我的表视图类被命名为&#34; SecondViewController&#34;
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = @"Cell";
cell = (StepperDetailsCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil){
cell = (StepperDetailsCell *)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
[cell.imagegroceries setImage:[UIImage imageNamed:[[self.didSelectedImages sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]objectAtIndex:indexPath.row]]];
[cell.objstepper addTarget:self action:@selector(stepperValuechanges:) forControlEvents:UIControlEventValueChanged];
cell.lblsteppervalue.text = [self.quantityArr objectAtIndex:indexPath.row];
return cell;
}
pragma mark - 步进行动方法
-(void)stepperValuechanges:(UIStepper *)sender{
CGPoint stepperPosition = [sender convertPoint:CGPointZero toView:self.tableView];
indexPath1 = [self.tableView indexPathForRowAtPoint:stepperPosition];
if (indexPath1 != nil )
{
float val = sender.value;
valueInt = (int)val;
[self.quantityArr replaceObjectAtIndex:indexPath1.row withObject:[NSString stringWithFormat:@"%i",valueInt]];
NSLog(@"%@",self.quantityMDict);
}
[self.tableView reloadData];
}
在这里,Everything正常工作,但每当从tableview底部加载新单元格时,那时新单元步进器就像消失的单元步进器一样。
答案 0 :(得分:0)
最后3天后我解决了我的问题。
在将UIStepper对象的操作触发到相应的Controller对象之前,只需设置UIStepper的值,如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"Cell";
cell = (StepperDetailsCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil){
cell = (StepperDetailsCell *)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
[cell.imagegroceries setImage:[UIImage imageNamed:[[self.didSelectedImages sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]objectAtIndex:indexPath.row]]];
//我将前一个值(此值,我从名为“quantityArr”的数组中取出)设置为相应的步进器,然后从步进器对象触发控制器对象。就是这样......
[cell.objstepper setValue:[[self.quantityArr objectAtIndex:indexPath.row] doubleValue]];
[cell.objstepper addTarget:self action:@selector(stepperValuechanges:) forControlEvents:UIControlEventValueChanged];
cell.lblsteppervalue.text = [self.quantityArr objectAtIndex:indexPath.row];
return cell;
}