如何在 UITableView Cell 中启用 UITextField userinteraction 属性
how to enable UITextField userinteraction property inside UITableView Cell
我有 UITableViewCell
,里面有 UITextfield
,我做了 7 个部分(动态),每个部分有一个单元格(一行),其中包括 UITextField
。
现在 UINavigationBar
的右上角有一个按钮(“编辑”按钮)。
我需要为所有部分文本字段启用用户交互。这怎么可能?
这是我的UITableviewCode
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
sharedManager=[Mymanager sharedManager];
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//UITableViewCell *cell;
UILabel *label = nil;
NSString *text;
cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil)
{
cell = [[timeLineDetailsCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"cell"];
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setTag:1];
[[label layer] setBorderWidth:2.0f];
[[cell contentView] addSubview:label];
}
if(indexPath.section==0){
text = [contents objectAtIndex:0];
}else if(indexPath.section==1){
text = [contents objectAtIndex:1];
}else if(indexPath.section==2){
text = [contents objectAtIndex:2];
}else if(indexPath.section==3){
text = [contents objectAtIndex:3];
}else if(indexPath.section==4){
text = [contents objectAtIndex:4];
}else if(indexPath.section==5){
text = [contents objectAtIndex:5];
}else if(indexPath.section==6){
text = [contents objectAtIndex:6];
}else if(indexPath.section==7){
text = [contents objectAtIndex:7];
}
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
if (!label)
label = (UILabel*)[cell viewWithTag:1];
[cell.ds1 setText:text];
[cell.ds1 setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text ;
if(indexPath.section==0){
text = [contents objectAtIndex:0];
}else if(indexPath.section==1){
text = [contents objectAtIndex:1];
}else if(indexPath.section==2){
text = [contents objectAtIndex:2];
}else if(indexPath.section==3){
text = [contents objectAtIndex:3];
}else if(indexPath.section==4){
text = [contents objectAtIndex:4];
}else if(indexPath.section==5){
text = [contents objectAtIndex:5];
}else if(indexPath.section==6){
text = [contents objectAtIndex:6];
}else if(indexPath.section==7){
text = [contents objectAtIndex:7];
}
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2);
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *str= [title objectAtIndex:section];
return str;
}
我的编辑按钮代码是
-(void)editTimeline{
cell.ds1.userInteractionEnabled=YES;
[cell.ds1 setUserInteractionEnabled:TRUE];
cell.ds1.layer.borderColor=[UIColor lightGrayColor].CGColor;
cell.ds1.layer.borderWidth=1;
// [cell.ds1 becomeFirstResponder];
}
当我点击“编辑”按钮时,只有一些行可以编辑。请帮助我。
define flag data memeber with Bool type
-(void)editTimeline{
flag=true;
[self.tableView reload];
}
add following condition in cellForRowAtIndexpath method
if(flag==true){
[cell.ds1 setUserInteractionEnabled:TRUE];
}
else{
[cell.ds1 setUserInteractionEnabled:NO];
}
let me know if it help u....!!!
我会使用这个代码 -
NSArray *cellArray=[itemsTable visibleCells];
for (UITableViewCell *aCell in cellArray)
{
UITextField *myTField=(UITextField*)yourtextField;
myTField.userInteractionEnabled=YES;
}
myTField
作为带有特定标签的子视图添加到单元格中。您可以通过 [aCell viewWithTag:someTag];
.
获得 myTField
为您的部分中的每一行调用 cellForRowAtIndexPath,使其无法被 tableView 选择(否则它会触发点击而不是您)并以您希望的方式为您返回的单元格启用用户交互:3
我有 UITableViewCell
,里面有 UITextfield
,我做了 7 个部分(动态),每个部分有一个单元格(一行),其中包括 UITextField
。
现在 UINavigationBar
的右上角有一个按钮(“编辑”按钮)。
我需要为所有部分文本字段启用用户交互。这怎么可能?
这是我的UITableviewCode
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
sharedManager=[Mymanager sharedManager];
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//UITableViewCell *cell;
UILabel *label = nil;
NSString *text;
cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil)
{
cell = [[timeLineDetailsCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"cell"];
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setTag:1];
[[label layer] setBorderWidth:2.0f];
[[cell contentView] addSubview:label];
}
if(indexPath.section==0){
text = [contents objectAtIndex:0];
}else if(indexPath.section==1){
text = [contents objectAtIndex:1];
}else if(indexPath.section==2){
text = [contents objectAtIndex:2];
}else if(indexPath.section==3){
text = [contents objectAtIndex:3];
}else if(indexPath.section==4){
text = [contents objectAtIndex:4];
}else if(indexPath.section==5){
text = [contents objectAtIndex:5];
}else if(indexPath.section==6){
text = [contents objectAtIndex:6];
}else if(indexPath.section==7){
text = [contents objectAtIndex:7];
}
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
if (!label)
label = (UILabel*)[cell viewWithTag:1];
[cell.ds1 setText:text];
[cell.ds1 setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text ;
if(indexPath.section==0){
text = [contents objectAtIndex:0];
}else if(indexPath.section==1){
text = [contents objectAtIndex:1];
}else if(indexPath.section==2){
text = [contents objectAtIndex:2];
}else if(indexPath.section==3){
text = [contents objectAtIndex:3];
}else if(indexPath.section==4){
text = [contents objectAtIndex:4];
}else if(indexPath.section==5){
text = [contents objectAtIndex:5];
}else if(indexPath.section==6){
text = [contents objectAtIndex:6];
}else if(indexPath.section==7){
text = [contents objectAtIndex:7];
}
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2);
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *str= [title objectAtIndex:section];
return str;
}
我的编辑按钮代码是
-(void)editTimeline{
cell.ds1.userInteractionEnabled=YES;
[cell.ds1 setUserInteractionEnabled:TRUE];
cell.ds1.layer.borderColor=[UIColor lightGrayColor].CGColor;
cell.ds1.layer.borderWidth=1;
// [cell.ds1 becomeFirstResponder];
}
当我点击“编辑”按钮时,只有一些行可以编辑。请帮助我。
define flag data memeber with Bool type
-(void)editTimeline{
flag=true;
[self.tableView reload];
}
add following condition in cellForRowAtIndexpath method
if(flag==true){
[cell.ds1 setUserInteractionEnabled:TRUE];
}
else{
[cell.ds1 setUserInteractionEnabled:NO];
}
let me know if it help u....!!!
我会使用这个代码 -
NSArray *cellArray=[itemsTable visibleCells];
for (UITableViewCell *aCell in cellArray)
{
UITextField *myTField=(UITextField*)yourtextField;
myTField.userInteractionEnabled=YES;
}
myTField
作为带有特定标签的子视图添加到单元格中。您可以通过 [aCell viewWithTag:someTag];
.
myTField
为您的部分中的每一行调用 cellForRowAtIndexPath,使其无法被 tableView 选择(否则它会触发点击而不是您)并以您希望的方式为您返回的单元格启用用户交互:3