2014-02-26 56 views
2

您好在桌面單元格中使用UIStepper。當UIStepper值改變,我必須顯示錶視圖cell..Here在標籤值的代碼iOS中的UIStepper UILabel

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     UILabel *lblQuantityValue = [[UILabel alloc] initWithFrame:CGRectMake(140, 410, 50, 30)]; 
     lblQuantityValue.text = @"1"; 
     lblQuantityValue.tag = indexPath.section; 
     lblQuantityValue.backgroundColor = [UIColor clearColor]; 
     [[lblQuantityValue layer] setCornerRadius:5]; 
     [[lblQuantityValue layer] setMasksToBounds:YES]; 
     [[lblQuantityValue layer] setBorderWidth:1.0f]; 
     [[lblQuantityValue layer] setBorderColor:[[UIColor orangeColor]CGColor]]; 
     [cell addSubview:lblQuantityValue]; 

     UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(200, 410, 50, 30)]; 
     [stepper addTarget:self action:@selector(stepper_ValueChanged:) for ControlEvents:UIControlEventValueChanged]; 
     stepper.tag = indexPath.row; 
     stepper.minimumValue = 1; 
     stepper.maximumValue = 10; 
     [cell addSubview:stepper]; 
} 

stepper_ValueChanged方法如下

-(void)stepper_ValueChanged:(UIStepper*)sender 
{ 
UILabel *lblQuantityValue = (UILabel*)[self.view viewWithTag:sender.tag]; 
int value2 = [sender value]; 
lblQuantityValue.text = [NSString stringWithFormat:@"%d", value2]; 
} 

我收到崩潰報告如下

-[UIView setText:]: unrecognized selector sent to instance 0x111e10640 
2014-02-26 11:22:24.159 Hyderabad Bazar[2115:a0b] *** Terminating app due to uncaught  exception 'NSInvalidArgumentException', reason: '-[UIView setText:]: unrecognized selector sent to instance 0x111e10640' 
*** First throw call stack: 
(
0 CoreFoundation      0x0000000101ce3795 __exceptionPreprocess + 165 
1 libobjc.A.dylib      0x00000001019bf991 objc_exception_throw + 43 
2 CoreFoundation      0x0000000101d74bad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 
3 CoreFoundation      0x0000000101cd509d ___forwarding___ + 973 
4 CoreFoundation      0x0000000101cd4c48 _CF_forwarding_prep_0 + 120 
5 Hyderabad Bazar      0x00000001000202a6 -[CartViewController stepper_ValueChanged:] + 390 
6 UIKit        0x0000000100628a8a -[UIApplication sendAction:to:from:forEvent:] + 104 
7 UIKit        0x0000000100628a20 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 17 
8 UIKit        0x00000001006fc91c -[UIControl _sendActionsForEvents:withEvent:] + 203 
9 UIKit        0x0000000100a895d7 -[UIStepper _updateCount:] + 338 
10 UIKit        0x0000000100a89346 -[UIStepper endTrackingWithTouch:withEvent:] + 32 
11 UIKit        0x00000001006fbe52 -[UIControl touchesEnded:withEvent:] + 472 
12 UIKit        0x000000010092cc0d _UIGestureRecognizerUpdate + 5149 
13 UIKit        0x000000010065d2e5 -[UIWindow _sendGesturesForEvent:] + 928 
14 UIKit        0x000000010065dfa5 -[UIWindow sendEvent:] + 910 
15 UIKit        0x0000000100637962 -[UIApplication sendEvent:] + 211 
16 UIKit        0x000000010062579f _UIApplicationHandleEventQueue + 9549 
17 CoreFoundation      0x0000000101c72e61 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 
18 CoreFoundation      0x0000000101c72732 __CFRunLoopDoSources0 + 242 
19 CoreFoundation      0x0000000101c8e5bf __CFRunLoopRun + 767 
20 CoreFoundation      0x0000000101c8ded3 CFRunLoopRunSpecific + 467 
21 GraphicsServices     0x000000010408a3a4 GSEventRunModal + 161 
22 UIKit        0x0000000100627a63 UIApplicationMain + 1010 
23 Hyderabad Bazar      0x00000001000274e3 main + 115 
24 libdyld.dylib      0x00000001028067e1 start + 0 

請幫我

回答

0

代碼分配indexPath.row作爲步進標籤。在行0(零)中,該標記爲零。 viewWithTag:返回接收者視圖層次結構中的第一個視圖,該視圖包含接收器,該標記爲。那麼問題是,

UILabel *lblQuantityValue = (UILabel*)[self.view viewWithTag:sender.tag]; 

...正在返回self.view,其標記無疑是零。快速解決方案是將標記設置爲第1行,並相應地調整查找。

+0

同意DANH,你推測viewWithTag是一個UILabel,但沒有保證,它實際上是。在將它作爲標籤對待之前調用([lblQuantityValue idKindOfClass:[UILabel class])可能會非常有幫助。 –

+0

另外一個潛在的基於標籤的災難是你如何分配標籤。使用該部分作爲標籤的標籤,將該行作爲步進器的標籤可以保證標籤碰撞。如果您要使用標籤,那麼您有責任確保所有標籤對於調用視圖和所有子視圖都是唯一的,一直沿着視圖層次結構。 –

0

試試這個,

-(void)stepper_ValueChanged:(UIStepper*)sender 
{ 
     int tag = sender.tag; 
     int labelTagInCell = //tag of label in cell 
     //assumes only 1 section 
     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:tag inSection:0]; 
     UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 
     UILabel *lblQuantityValue = (UILabel*)[cell viewWithTag:labelTagInCell]; 
     int value2 = [sender value]; 
     lblQuantityValue.text = [NSString stringWithFormat:@"%d", value2]; 
} 

這將是更好的使用自定義單元格,即:從該類步進的UITableViewCell和控制值變化的子類。

CustomCell.h

@property (strong, nonatomic) UILabel *lblQuantityValue; 
@property (strong, nonatomic) UIStepper *stepper; 

CustomCell.m

- (void)awakeFromNib { 
    [super awakeFromNib]; 
     self.lblQuantityValue = [[UILabel alloc] initWithFrame:CGRectMake(140, 410, 50, 30)]; 
     self.lblQuantityValue.text = @"1"; 
     self.lblQuantityValue.tag = indexPath.section; 
     self.lblQuantityValue.backgroundColor = [UIColor clearColor]; 
     [[self.lblQuantityValue layer] setCornerRadius:5]; 
     [[self.lblQuantityValue layer] setMasksToBounds:YES]; 
     [[self.lblQuantityValue layer] setBorderWidth:1.0f]; 
     [[self.lblQuantityValue layer] setBorderColor:[[UIColor orangeColor]CGColor]]; 
     [self addSubview:lblQuantityValue]; 

     self.stepper = [[UIStepper alloc] initWithFrame:CGRectMake(200, 410, 50, 30)]; 
     [self.stepper addTarget:self action:@selector(stepper_ValueChanged:) for ControlEvents:UIControlEventValueChanged]; 
     self.stepper.tag = indexPath.row; 
     self.stepper.minimumValue = 1; 
     self.stepper.maximumValue = 10; 
     [self addSubview:stepper]; 
} 

-(void)stepper_ValueChanged:(UIStepper*)sender 
{ 
     int value2 = [self.stepper value]; 
     self.lblQuantityValue.text = [NSString stringWithFormat:@"%d", value2]; 
} 
1

兩個,你可能會發現更多可行的不同的方法:

子類的UITableViewCell,在UIStepper和的UILabel添加作爲電池的特性,並將該單元添加爲UISteppers valueChanged操作的目標,並在該值更改時更新該標籤。這取決於你如何希望堅持的東西,這有可能會靠不住與出隊和prepareForReuse這給我們帶來了其他的想法...

OR

創建一個適當的類變量或屬性的NSMutableArray持有的The文本對於所有的細胞,無論它們是否可見。標籤總是從該數組中填充,而不是直接從步進器中填充。你仍然可以使用一個目標/行動對有從UIStepper的valueChanged事件的細胞反應,但你的電纜鋪設工作的tableView作爲細胞的委託,並使用協議,從細胞撥打電話這樣的:

[self.tableViewDelegate newValue:value forCell:self]; 

然後讓的tableView實現協議的方法是這樣的:

- (void)newValue:(NSString*)value forCell:(CustomCellClassName*)cell 
{ 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
    //now use the index path to set the "value" parameter at the right place in your class variable or property 

    //Now that the data source is updated, refresh the table 
    [self.tableView reloadData]; 
}