1

如何根据索引将值放入特定行的特定文本字段中?我的结果只在底部或最后一个单元格更新......

目前,它要求自己获取当前值。我对这个问题的唯一问题是它不会更新当前行,而是更新索引路径的最后一行。

在此处输入图像描述

.h 文件

//当label1和label 2连接时会导致程序崩溃。连接不能将原型插座作为目标

@property (nonatomic, strong) IBOutlet UILabel *label1; //not connected
@property (nonatomic, strong) IBOutlet UILabel *label2; //not connected


@property (nonatomic, weak) IBOutlet UIStepper *stepper; // not conneted
-(IBAction)stepperChangedValue:(UIStepper *)sender; //connected to uistepper

.m 文件

-(IBAction)stepperChangedValue:(UIStepper *)sender{

//my incremental number is here
NSInteger i = [sender value];
NSLog(@"value: %i",i);
UITextField *textField = (UITextField*)[self.view viewWithTag:2];
textField.text = [NSString stringWithFormat:@"%d",i ];



//find row #
UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
NSLog(@"value: %i",indexPath.row); //I have my row index here


//How do I put my sender value back into the correct row of my table?
}

// Return number of sections in table (always 1 for this demo!)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [myData count];
}

// Return the amount of items in our table (the total items in our array above)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [myData count];
}

// Return a cell for the table
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // A cell identifier which matches our identifier in IB
    static NSString *CellIdentifier = @"StepperCell";

    // Create or reuse a cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Get the cell label using it's tag and set it
    UILabel *cellLabel = (UILabel *)[cell viewWithTag:1];
    [cellLabel setText:[myData objectAtIndex:indexPath.row]];

    // get the cell label using it's tag and set it
    UILabel *cellLabel2 = (UILabel *)[cell viewWithTag:2];
    [cellLabel2 setText:@"0"];

    return cell;
}

多谢你们!我很感激。

4

2 回答 2

2
-(IBAction)stepperChangedValue:(UIStepper *)sender{

   //find row #
   UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
   NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
   NSLog(@"value: %i",indexPath.row); //I have my row index here

    //my incremental number is here
    NSInteger i = [sender value];
    NSLog(@"value: %i",i);
   cell.textField.text = [NSString stringWithFormat:@"%d",i ]; // textField is an outlet for your textfield in your cell

}

你的代码应该是这样的。您需要textfield在获得cell. 在您的代码中,您正在更改textfieldwithtag 2和 lastcell textfield的文本tag 2。所以它也会导致问题。

于 2013-02-17T06:10:10.583 回答
0

您需要使用UITableViewCell该类。声明label,outletaction每个类的方法。

自定义单元格.h

@property (weak, nonatomic) IBOutlet UILabel *lbl_title;

@property (weak, nonatomic) IBOutlet UIStepper *stepper;

@property (weak, nonatomic) IBOutlet UILabel *lbl_value;

- (IBAction)stepper:(UIStepper *)sender;

自定义单元格.m

- (IBAction)stepper:(UIStepper *)sender {
NSUInteger value = sender.value;

 lbl_value.text =[NSString stringWithFormat:@"%d",value];


}
于 2016-02-01T04:11:48.263 回答