菜鸟教程小白 发表于 2022-12-11 22:39:44

objective-c - UITableView滚动后UITextFields重叠


                                            <p><p>我有一个带有 UITextFields 的 UITableView 来制作表单。它看起来很棒——除非你滚动新的 UITextFields 被放在旧的 UITextFields 之上......我不知道为什么!我知道当单元格被重用时(即 dequeueReusableCellWithIdentifier 返回非零),单元格与现有的 UITextField 返回。我找到了一个解决方案来保持标签的唯一性,最好删除任何以前的 UITextField。但我不想那样做。在提交此表单的最后,我想从文本字段中获取所有值(如果我删除它们就不能这样做)..</p>

<p>这里是代码。任何帮助表示赞赏!</p>

<pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @&#34;Cell&#34;;

UITableViewCell *cell = (UITableViewCell *) ;

if (cell == nil)
    cell = [ initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

cell.accessoryType = UITableViewCellAccessoryNone;

UITextField *playerTextField = [ initWithFrame:CGRectMake(155, 15, 130, 30)];
playerTextField.adjustsFontSizeToFitWidth = YES;
playerTextField.textColor = ;

if (indexPath.row == 0) {
    playerTextField.placeholder = @&#34;Unique ID Sample&#34;;
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyNext;
}

else if (indexPath.row == 1) {
    playerTextField.placeholder = @&#34;Common Name&#34;;
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if (indexPath.row == 2) {
    playerTextField.placeholder = @&#34;Scientific Name&#34;;
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ( == 3) {
    playerTextField.placeholder = @&#34;03/25/1992&#34;;
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ( == 4) {
    playerTextField.placeholder = @&#34;Male&#34;;
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ( == 5) {
    playerTextField.placeholder = @&#34;03/01/2012&#34;;
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ( == 6) {
    playerTextField.placeholder = @&#34;Huntington, WV&#34;;
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ( == 7) {
    playerTextField.placeholder = @&#34;Huntington, WV&#34;;
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ( == 8) {
    playerTextField.placeholder = @&#34;Marshall University&#34;;
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}
else if ( == 9) {
    playerTextField.placeholder = @&#34;Sub Straight&#34;;
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ( == 10) {
    playerTextField.placeholder = @&#34;Any Light&#34;;
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ( == 11) {
    playerTextField.placeholder = @&#34;Temperature&#34;;
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ( == 12) {
    playerTextField.placeholder = @&#34;A Lot&#34;;
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ( == 13) {
    playerTextField.placeholder = @&#34;All Types&#34;;
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ( == 14) {
    playerTextField.placeholder = @&#34;Schedule&#34;;
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ( == 15) {
    playerTextField.placeholder = @&#34;MM/DD/YYYY&#34;;
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ( == 16) {
    playerTextField.placeholder = @&#34;Ate Someone&#34;;
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ( == 17) {
    playerTextField.placeholder = @&#34;MM/DD/YYYY&#34;;
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ( == 18) {
    playerTextField.placeholder = @&#34;The Morgue&#34;;
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ( == 19) {
    playerTextField.placeholder = @&#34;MM/DD/YYYY&#34;;
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ( == 20) {
    playerTextField.placeholder = @&#34;MM/DD/YYYY&#34;;
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}

else if ( == 21) {
    playerTextField.placeholder = @&#34;To/ From&#34;;
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyDone;
}   

playerTextField.backgroundColor = ;
playerTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
playerTextField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
playerTextField.textAlignment = UITextAlignmentLeft;
playerTextField.tag = 0;

playerTextField.clearButtonMode = UITextFieldViewModeNever; // no clear &#39;x&#39; button to the right
;

    ;

;

cell.textLabel.text = ;


return cell;   
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您将不得不创建一个自定义 <code>UITableViewCell</code>。这将允许您使用可以直接访问的 <code>UITextField</code> 创建一个 <code>ivar</code>。这样,当单元格出列并重用时,您只需在需要时进行设置。请务必在您的单元子类中覆盖 <code>prepareForReuse:</code> 并重置您添加的组件的所有属性。</p>

<p>上面的代码可以帮助你的另一件事是使用 <code>typedef enum</code> 而不是使用整数来确定它是什么类型的单元格:</p>

<pre><code>typedef enum{
    PlayerFieldId = 0,
    PlayerFieldName,
    PlayerFieldGender
} PlayerFields
</code></pre>

<p>然后在您的自定义单元格中,您可以通过上面的枚举和 switch 语句设置单元格的类型:</p>

<pre><code>switch(type){
case PlayerFieldId:
    playerTextField.placeholder = @&#34;Unique ID Sample&#34;;
    playerTextField.keyboardType = UIKeyboardTypeDefault;
    playerTextField.returnKeyType = UIReturnKeyNext;
    break;
    /* other fields here */
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于objective-c - UITableView滚动后UITextFields重叠,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/10112320/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/10112320/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: objective-c - UITableView滚动后UITextFields重叠