繁体   English   中英

在cellForRowAtIndexPath中的单元格中设置UITextField占位符

[英]Setting UITextField placeholder in a cell from cellForRowAtIndexPath

我有一个带有自定义单元格的UITableView。 在单元格内部,我有一个带有一些占位符文本的UITextField。 我想从cellForRowAtIndexPath设置占位符,但是当我尝试设置它时,占位符只是说(null)。 此外,我想将占位符的格式设置为“ Period(单元格位于tableView中的数字)”,因此对于离tableView顶部最近的单元格,它会显示“ Period 1”,从顶部开始的第二个单元格会说“期间2”。 我不知道如何进行编号或为什么单元格打印为空。 这是代码-

在自定义单元中-

NSString *rowString = [NSString stringWithFormat:@"Period %@", self.rowNumber];
self.classText = [[UITextField alloc] init];
self.classText.delegate = self;
self.classText.placeholder = rowString;
self.classText.frame = CGRectMake(14, 3, 320, 40);
self.classText.keyboardAppearance = UIKeyboardAppearanceDark;
self.classText.font = [UIFont fontWithName:@"HelveticaNeue" size:17];
//[self.classText addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[self addSubview:self.classText];

在cellForRowAtIndex路径中-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    SchoolCell *cell = nil;
    static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
    cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
    if (cell == nil) {
        cell = [[SchoolCell alloc]
                initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.rowNumber = @"test";

    return cell;
}

您将在这些自定义单元中的哪些行放置用于初始化classText的代码? 您可能会在初始化classText之后在cellForRowAtIndex方法中设置rowNumber。 创建一个接受rowNumber作为参数的自定义init方法,或者在rowString中创建一个方法,稍后您可以在设置rowNumber之后从cellForRowAtIndex调用它,并将classText初始化代码放入其中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM