我已經創建了包含用戶名和密碼的登錄頁面。我創建了一個自定義單元格,它有一個標籤和一個文本字段。我創建了一個分組表格視圖並使用了自定義單元格。其實我已經成功創建,但問題是,我如何獲取用戶名和密碼的文本字段值。 Bcoz我在這兩個領域只使用了一個文本框。我無法正確獲取文本字段值。我總是得到這兩個領域的最後價值。我如何獲得用戶名並正確傳遞單詞文本值?如何在iPhone的表格視圖中獲取文本字段值?
這裏我的示例代碼,
在自定義單元格,
@interface CustomCell : UITableViewCell {
UILabel *userLbl;
UITextField *userTxt;
}
@property (nonatomic, retain) IBOutlet UILabel *userLbl;
@property (nonatomic, retain) IBOutlet UITextField *userTxt;
@end
在根視圖控制器,
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
for(id currentObjects in nibObjects)
{
if([currentObjects isKindOfClass:[CustomCell class]])
{
cell = (CustomCell *) currentObjects;
}
}
}
if (indexPath.section == 0) {
if(indexPath.row == 0)
{
[[cell userLbl] setText:@"User Name:"];
cell.userTxt.tag = 0;
//self.userName = [[cell userTxt] text];
}
if (indexPath.row == 1) {
[[cell userLbl] setText:@"Pass Word:"];
cell.userTxt.secureTextEntry = YES;
//self.passWord = [[cell userTxt] text];
cell.userTxt.tag = 1;
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// Configure the cell.
return cell;
}
-(IBAction) submitAction : (id) sender
{
self.userName = [[cell userTxt] text];
self.passWord = [[cell userTxt] text];
[cell.userTxt resignFirstResponder];
NSLog(@"The user Name is %@", self.userName);
NSLog(@"The password is %@", self.passWord);
//Doesn't work
/* UITextField *txtField = (UITextField*)[cell viewWithTag:0];
self.userName = [txtField text];
UITextField *txtField1 = (UITextField*)[cell viewWithTag:1];
self.passWord = [txtField1 text];
NSLog(@"The user Name is %@", self.userName);
NSLog(@"The password is %@", self.passWord);*/
}
這裏我對圖像的屏幕快照,
請幫助我。
謝謝。
我有同樣的問題 – Siva 2013-10-28 07:01:45