ahe*_*ang 4 iphone objective-c ios
我有以下代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (indexPath.row == 0)
cell.tag = 0;
else {
cell.tag = 1;
}
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *startDtLbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 80, 25)];
if (indexPath.row == 0)
startDtLbl.text = @"Username";
else {
startDtLbl.text = @"Password";
}
startDtLbl.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:startDtLbl];
UITextField *passwordTF = [[UITextField alloc] initWithFrame:CGRectMake(100, 5, 200, 35)];
passwordTF.delegate = self;
if (indexPath.row == 0)
passwordTF.tag = 2;
else {
passwordTF.tag = 3;
}
[cell.contentView addSubview:passwordTF];
}
return cell;
}
Run Code Online (Sandbox Code Playgroud)
我想获得UITextField,我该怎么做?我尝试过以下操作但失败了:
UITableViewCell * username_cell = (UITableViewCell*)[self.view viewWithTag:0];
UITableViewCell * password_cell = (UITableViewCell*)[self.view viewWithTag:1];
UITextField * username = (UITextField*)[username_cell.contentView viewWithTag:2];
UITextField * password = (UITextField*)[password_cell.contentView viewWithTag:3];
NSLog(@"Username is %@", [username text]);
NSLog(@"Password is %@", [password text]);
Run Code Online (Sandbox Code Playgroud)
你应该停止使用标签来获取细胞.您应该使用indexPaths.
更换
UITableViewCell * username_cell = (UITableViewCell*)[self.view viewWithTag:0];
UITableViewCell * password_cell = (UITableViewCell*)[self.view viewWithTag:1];
Run Code Online (Sandbox Code Playgroud)
同
NSIndexPath *indexPathUserName = [NSIndexPath indexPathForRow:0 inSection:0];
UITableViewCell * username_cell = [self.tableView cellForRowAtIndexPath:indexPathUserName];
NSIndexPath *indexPathPassword = [NSIndexPath indexPathForRow:1 inSection:0];
UITableViewCell * password_cell = [self.tableView cellForRowAtIndexPath:indexPathPassword];
Run Code Online (Sandbox Code Playgroud)
当您要引用特定视图时,不能使用标记0.因为所有没有自定义标记的标记都具有0的标记.因此,如果使用viewWithTag:0,则会获得最后添加的视图.而且通常这不是你想要的观点.
| 归档时间: |
|
| 查看次数: |
10008 次 |
| 最近记录: |