按钮上的UITableView单元格单击编辑UITextView值

时间:2016-09-07 07:44:59

标签: ios objective-c tableview

我想知道在使用目标C按下按钮时如何编辑文本框。

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

        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {

            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
        }

        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        Property *objProperty=nil;
        ComponentLog *objCompLog=nil;

        if(tableView==tblProperties)
            objProperty=[arrProperties objectAtIndex:indexPath.row];
        else
            objCompLog=[arrCompLogs objectAtIndex:indexPath.row];


        UILabel *lblTag=[[UILabel alloc]init];
        lblTag.frame=CGRectMake(setX, 20.0, 150.0, 60.0);
        [lblTag setTextColor:[UIColor colorWithRed:0.0 green:73.0/255.0 blue:121.0/255.0 alpha:1.0]];
        lblTag.numberOfLines=2;

        if(tableView==tblProperties)
            [lblTag setText:[objProperty propertyTypeName]];
        else
        {
            NSString *d=[objCompLog dateAndTime];
            NSLog(@"%@",d);
            NSArray *arrSeparate = [d componentsSeparatedByString:@"T"];
            NSString *str1 = [arrSeparate objectAtIndex:0];

            if(str1 != nil && ![str1 isEqualToString:(NSString *)@""])
            {
                NSArray *arrDate=[str1 componentsSeparatedByString:@"-"];
                NSString *strYear=[arrDate objectAtIndex:0];
                NSString *strMonth=[arrDate objectAtIndex:1];
                NSString *strDay=[arrDate objectAtIndex:2];
                str1=[strMonth stringByAppendingString:@"/"];
                str1=[str1 stringByAppendingString:strDay];
                str1=[str1 stringByAppendingString:@"/"];
                str1=[str1 stringByAppendingString:strYear];
            }
            [lblTag setText:str1];
        }

        [lblTag setFont:[UIFont boldSystemFontOfSize:20.0]];
        [lblTag setBackgroundColor:[UIColor clearColor]];
        [cell.contentView addSubview:lblTag];
        [lblTag release];
        if(tableView==tblProperties)
        {
            UILabel *lblPropertyDesc=[[UILabel alloc]init];
            lblPropertyDesc.frame=CGRectMake(setAnotherX, 20.0, setPropertyDescWidth, 60.0);
            lblPropertyDesc.numberOfLines=2;
            [lblPropertyDesc setTextColor:[UIColor colorWithRed:0.0 green:73.0/255.0 blue:121.0/255.0 alpha:1.0]];
            [lblPropertyDesc setText:[objProperty propertyTypeDescription]];
            [lblPropertyDesc setFont:[UIFont boldSystemFontOfSize:20.0]];
            [lblPropertyDesc setBackgroundColor:[UIColor clearColor]];
            [cell.contentView addSubview:lblPropertyDesc];
            [lblPropertyDesc release];
        }
        else
        {
            lblDescriptionTag=[[UILabel alloc]init];
            lblDescriptionTag.frame=CGRectMake(setAnotherX, 20.0, setLogDescWidth, 60.0);
            lblDescriptionTag.numberOfLines=2;
            [lblDescriptionTag setTextColor:[UIColor colorWithRed:0.0 green:73.0/255.0 blue:121.0/255.0 alpha:1.0]];

            UILabel *lblDescription=[[UILabel alloc]init];
            lblDescription.frame=CGRectMake(setAnotherX, 20.0, setLogDescWidth, 60.0);
            lblDescription.numberOfLines=2;
            [lblDescription setTextColor:[UIColor colorWithRed:0.0 green:73.0/255.0 blue:121.0/255.0 alpha:1.0]];


            NSString* descHtml = [self flattenHTML:(NSString *)[objCompLog logDescription]];
            [lblDescription setText:descHtml];
            [lblDescription setFont:[UIFont boldSystemFontOfSize:20.0]];
            [lblDescription setBackgroundColor:[UIColor clearColor]];
            [cell.contentView addSubview:lblDescription];
            [lblDescription release];
        }

        btnEditValue = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnEditValue setFrame:CGRectMake(self.view.frame.size.width-150,25,50, 50.0)];
            btnEditValue.tag = indexPath.row;
            [btnEditValue setImage:[UIImage imageNamed:@"EditValue.png"] forState:UIControlStateNormal];
            [btnEditValue addTarget:self action:@selector(buttonEdit:
                                                      ) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:btnEditValue];


        btnSave = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnSave setFrame:CGRectMake(self.view.frame.size.width-60,25,50, 50.0)];
        [btnSave setImage:[UIImage imageNamed:@"done.png"] forState:UIControlStateNormal];
        [btnSave addTarget:self action:@selector(buttonSave) forControlEvents:UIControlEventTouchUpInside];
         btnSave.userInteractionEnabled=NO;
        [cell.contentView addSubview:btnSave];
        txtValue=[[[UITextView alloc]init]autorelease];
        txtValue.frame=CGRectMake(setValueX,20.0, 100, 60.0);
        [txtValue setTextColor:[UIColor colorWithRed:0.0 green:73.0/255.0 blue:121.0/255.0 alpha:1.0]];
        UIColor *borderColor = [UIColor colorWithRed:0.0 green:73.0/255.0 blue:121.0/255.0 alpha:1.0];
        txtValue.layer.borderColor = borderColor.CGColor;
        txtValue.layer.borderWidth = 1.0;
        txtValue.layer.cornerRadius = 5.0;
        if(tableView==tblProperties)
        [txtValue setText:[objProperty propValue]];
        [txtValue setFont:[UIFont boldSystemFontOfSize:20.0]];
        [txtValue setBackgroundColor:[UIColor clearColor]];
        txtValue.userInteractionEnabled = NO;

        [cell.contentView addSubview:txtValue];
         return cell;
        }
        @catch (NSException *ex) {
            [Utils LogExceptionOnServer:@"ComponentDetailViewController" methodName:@"cellForRowAtIndexPath" exception:[ex description]];
        }
    }
    -(void)buttonEdit:(UIButton *)sender
    {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
        UITableViewCell *cell = [tblProperties cellForRowAtIndexPath:indexPath];
        [cell.contentView bringSubviewToFront:txtValue];
        txtValue.userInteractionEnabled = YES;
        btnSave.userInteractionEnabled=YES;
        [txtValue becomeFirstResponder];
    }

1 个答案:

答案 0 :(得分:0)

从代码行中删除自动释放

txtValue=[[[UITextView alloc]init]autorelease];