English 中文(简体)
添加一门可转让的电文 单元仅显示案文 LAST电池的现场
原标题:Adding a UITextField to a UITableViewCell only shows a textField in the LAST cell

我的表格认为,牢房很多。 每个牢房都有自己的UIText油田。 我在方案上增加了案文领域。 我想每个案文。 ed子被击中时,现场出现。 (现在的表格采用编辑方式),当再次施加压力时,我想所有文本领域都会消失(打破编辑模式)。 我知道,我可以使用<条码>隐藏的财产实现这一点,但我尝试采用这种方法:

    - (IBAction)editButton:(id)sender 
{
    if (self.editing) 
    {
        [self setEditing:NO animated:YES];
        [self.myTableView setEditing:NO animated:YES];
        EditButton.title = @"Edit";
        cellText.hidden = YES;  //<-- THIS IS THE CODE
    } 
    else 
    {
        [self setEditing:YES animated:YES];
        [self.myTableView setEditing:YES animated:YES];
        EditButton.title = @"Done";
        cellText.hidden = NO;  //<-- THIS IS THE CODE
    }
}

但它只显示并隐藏了VERY LAST小组的文字。 怎样才能将其带到哪里,然后又会展示EVERY手机的正文。 thanks!

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
     {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];


         cellText = [[UITextField alloc]init];
         [cellText setFrame:CGRectMake(190, 15, 55, 30)];
         cellText.text = @"1";
         cellText.borderStyle = UITextBorderStyleRoundedRect;
         cellText.hidden = YES;
         cellText.userInteractionEnabled = NO;
         [cell addSubview:cellText];
    }    

return cell;
}

提前感谢!

最佳回答

You can get rid of this issue, using this trick, I am not sure as if it would create the memory leak within your code.Since, it create new cell each time.But surely you can use it,if you don t get some proper way of doing it. ;)

- (IBAction)editButton:(id)sender 
{
 if (self.editing) 
        {
            [self setEditing:NO animated:YES];
            [self.myTableView setEditing:NO animated:YES];
            EditButton.title = @"Edit";

        } 
        else 
        {
            [self setEditing:YES animated:YES];
            [self.myTableView setEditing:YES animated:YES];
            EditButton.title = @"Done";

        }
        [self.myTableView reloadData];
}

在重载了表格后,检查了ForRowAtIndexPath的病情,或者将自我编辑的价值传递给文本现场,使之隐藏/how。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];


    cellText = [[UITextField alloc]init];
    [cellText setFrame:CGRectMake(190, 15, 55, 30)];
    cellText.text = @"1";
    cellText.borderStyle = UITextBorderStyleRoundedRect;
    cellText.hidden = YES;
    cellText.backgroundColor = [UIColor redColor];
    cellText.userInteractionEnabled = NO;
    [cell addSubview:cellText];

    cellText.hidden=!self.editing;

    return cell;
}
问题回答

虽然你为每个牢房设立了一个文本领域,但仅提及点名的Ivar编码cellText。 因此,你只展示/宣传文本。

I suggest you reload the table when toggling the editing mode, and set the text fields visibility in tableView:cellForRowAtIndexPath:.

Oh, and you should release cellText after adding it as a subview. Otherwise you are leaking memory. And it is highly recommended that you add subviews to UITableViewCell content view, rather than directly to the cell.

D. 守则

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}



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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UITextField * cellText = [[UITextField alloc] initWithFrame:CGRectMake(1, 1, 100, 30)];
cellText.tag = 1;
cellText.textColor = [UIColor darkTextColor];
//cellText.numberOfLines = 0;
cellText.font = [ UIFont fontWithName: @"Helvetica-Bold" size: 12.0 ] ; 
cellText.backgroundColor = [ UIColor clearColor ] ;
cellText.text = @"123";
    cellText.hidden = YES;
[cell.contentView addSubview:cellText];
[cellText release];
cellText =nil;

// Set up the cell...


return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
// Detemine if it s in editing mode
  UITextField *cellText = (UITextField *)[[aTableView cellForRowAtIndexPath:indexPath] viewWithTag:1];
if (!self.editing) 
{
    [self setEditing:NO animated:YES];
    [self.tableView setEditing:NO animated:YES];
   // EditButton.title = @"Edit";

    cellText.hidden = YES;  //<-- THIS IS THE CODE
} 
else 
{
    [self setEditing:YES animated:YES];
    [self.tableView setEditing:YES animated:YES];
  //  EditButton.title = @"Done";
    cellText.hidden = NO;  //<-- THIS IS THE CODE
}

return UITableViewCellEditingStyleNone;
}

朋友们,这一法典正在对我进行罚款,相信你也有一天的 gr。

这实际上是正常的。 补充材料下的 Apple果文件:

Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.

因此,它将在囚室到达最后牢房之前,继续拆除牢房。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签