[英]Adding a UITextField in a UITableview Basics
我剛剛剛開始使用Xcode Objective-C,現在我正在嘗試創建一個帶有文本字段以供鍵入的表格視圖。 Ive研究了其他堆棧溢出問題,但許多問題是6到8年前的,似乎答案很廣,而且非常復雜。 有人可以幫助我了解如何在表格視圖中插入文本字段並給我一些建議的基礎知識。 謝謝!
在uitableview單元格中編寫此代碼
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.clearsOnBeginEditing = NO;
textField.textAlignment = UITextAlignmentRight;
textField.delegate = self;
[aCell.contentView addSubview:txt];
您可以按照以下步驟進行操作:
我建議您參考類似的教程
1. https://videos.raywenderlich.com/courses/22-table-views-in-ios/lessons/8 2. https://www.appcoda.com/expandable-table-view/
他們擁有最好的教程,其中包含所有步驟,您可以輕松執行所需的任何操作。
希望對您有幫助。
謝謝。
試試下面的代碼
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell == nil) {
/*
* Actually create a new cell (with an identifier so that it can be dequeued).
*/
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
/*
* Now that we have a cell we can configure it to display the data corresponding to
* this row/section
*/
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(45, 30, 200, 40)];
tf.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
tf.font = [UIFont fontWithName:@"Helvetica-Bold" size:25];
tf.backgroundColor=[UIColor whiteColor];
tf.text=@"Hello World";
[cell.contentView addSubview:tf];
/* Now that the cell is configured we return it to the table view so that it can display it */
return cell;
}
您可以創建一個自定義單元。 在其中添加文本字段,並通過注冊筆尖或類在表中使用該文本字段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.