我在一个应用程序中工作,我有tableview,其中包含Textfield和Label。现在我想要的是当我在文本字段中输入有分数的文本时,它应该计算一些东西,并在该单元格的tableview标签中给出结果百分比。 下面是我如何在cellForRowAtIndexpath中创建tetfield和label。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
lblpercent = [[UILabel alloc]initWithFrame:CGRectMake(395, 5, 270, 30)];
UITextField *txtscore = [[UITextField alloc] initWithFrame: CGRectMake(306,5,100,30)];
txtscore.delegate = self;
txtscore.keyboardType = UIKeyboardTypeNumberPad;
[txtscore addTarget:self action:@selector(textFieldDone:) forControlEvents:UIControlEventEditingDidEnd];
lblpercent.text = per;
}
对于caqlculation,我使用以下代码
-(void) textFieldDone: (id) sender
{
UITextField *field = sender;
NSLog(@"%d",i);
NSString *total = field.text;
int tot = [total intValue];
NSLog(@"The text is %d", tot);
per = [[NSString alloc] init];
if (tot == 90) {
percent=90;
}
per = [NSString stringWithFormat:@"%d",percent];
}
如何解决这个问题?
答案 0 :(得分:1)
我有一个好主意,如果不是你的问题,我肯定不会尝试,所以谢谢你;)
如果您不需要其他任何设置标签的文本字段的委托方法,您可以通过这种方式解决问题。
在UILabel上创建一个类别
@interface UILabel (CopyTextField) <UITextFieldDelegate>
@end
@implementation UILabel (CopyTextField)
-(void)textFieldDidEndEditing:(UITextField *)textField
{
// do whatever you want with your textfield's text and set self.text to the value
self.text = textField.text; // here I'm just copying the text as it is
}
@end
另一方面,您需要将标签设置为textField的委托(import UILabel + CopyTextField.h)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UILabel *lblpercent = [[UILabel alloc]initWithFrame:CGRectMake(395, 5, 270, 30)];
UITextField *txtscore = [[UITextField alloc] initWithFrame:CGRectMake(306,5,100,30)];
txtscore.delegate = lblpercent;
txtscore.keyboardType = UIKeyboardTypeNumberPad;
}