如何使用来自UITextField的用户输入添加一个新的UITableViewCell与图像?

问题描述:

我越来越沮丧,因为我无法弄清楚如何让这个工作。 我已经搜索和搜索,没有什么似乎帮助我得到这个做我想做的事情。如何使用来自UITextField的用户输入添加一个新的UITableViewCell与图像?

我在尝试完成:我正在尝试获取用户将在顶部使用UITextField键入的任务,以便将其输入添加为文本,并在其左侧添加复选框。当单元格被轻敲时,我想让复选框更改为一个带有复选标记的复选框,并且整个单元格将不透明度更改为50%。另外,我想在从左向右滑动时使单元格弹出一个删除按钮。

我会如何让这一切发生?我目前只是使用Attribute Inspector将UITextField添加到.xib中,以使其看起来像它一样。我也有一个工具栏,我已经使用了Attributes Inspector进行修改。

什么都没有编码连接这些行为,他们只是坐在.xib。

这是我创建的渲染图像。 (我想获得)

http://i.imgur.com/rTKnvud.png

这里是我迄今为止在Xcode中的.xib。

http://i.imgur.com/wfcVOrY.png

谢谢先进, 雅各

+0

本教程http://adeem.me/blog/2009/05/30/iphone-sdk-tutorial-part-6-creating-custom-uitableviewcell-using-interface-builder-uitableview/ –

你需要或者添加按钮编程细胞或创建一个自定义单元格。当用户点击单元格或复选框时,您可以将按钮的图像更改为选定的复选框。为了正确显示复选框,需要一个NSMutableArray并使用下面的代码。

注意

1. specialNeedsList is a array which I'm using to display data on cell. 
2. selectedSpecialNeed is a Mutable Array which I'm using to store selected/checked values 
3. I'm using checkbox button for UITableViewCell accessory view. IN your case code will be little bit different. 

按照本教程Get row for button tapped以及我的代码一起。你会得到这个想法。 无论我在做什么- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 你需要在UIButton的动作上做到这一点。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    NSInteger returnValue = 1; 
    return returnValue; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSInteger returnValue = 0; 
    returnValue = ([self.specialNeedsList count] > 0) ? [self.specialNeedsList count]:0; 
    return returnValue; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 

      UIButton * btnShare = [[UIButton alloc] initWithFrame:CGRectMake(0,0,30,30)]; 
      [btnShare setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal]; 
      [btnShare setImage:[UIImage imageNamed:@"checkboxChecked.png"] forState:UIControlStateHighlighted]; 
      [btnShare setImage:[UIImage imageNamed:@"checkboxChecked.png"] forState:UIControlStateSelected]; 
      [btnShare addTarget: self action: @selector(accessoryButtonTapped:withEvent:) 
       forControlEvents: UIControlEventTouchUpInside]; 

      cell.accessoryView = btnShare; 
      [btnShare release]; 
     } 
    NSString* subAreaOfStudy = [self.specialNeedsList objectAtIndex:indexPath.row]; 
    if (self.specialNeedsList && [self.specialNeedsList count] > 0) { 
     [cell.textLabel setFont:[UIFont systemFontOfSize:15.0f]]; 
     cell.textLabel.text = [self.specialNeedsList objectAtIndex:indexPath.row]; 
    } 
    BOOL isShared = ([self.selectedSpecialNeeds count] > 0 && ([self.selectedSpecialNeeds indexOfObject:subAreaOfStudy] != NSNotFound)); 

    UIButton* btnShare = (UIButton*)cell.accessoryView; 
    btnShare.selected = isShared; 
    [btnShare setNeedsDisplay]; 

    return cell; 

} 

- (void) accessoryButtonTapped: (UIControl *) button withEvent: (UIEvent *) event{ 

    NSIndexPath * indexPath; 
    indexPath = [tblVSpecialNeeds indexPathForRowAtPoint: [[[event touchesForView: button] anyObject] locationInView:tblVSpecialNeeds]]; 
    if (indexPath == nil) 
     return; 

    [self tableView:tblVSpecialNeeds accessoryButtonTappedForRowWithIndexPath:indexPath]; 
} 

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{ 
    NSString *draft = [self.specialNeedsList objectAtIndex:indexPath.row]; 

    BOOL recordAlreadySelected = ([self.selectedSpecialNeeds indexOfObjectIdenticalTo:draft] != NSNotFound); 
    if(recordAlreadySelected) { 
     [self.selectedSpecialNeeds removeObject:draft]; 

    } 
    else { 
     [self.selectedSpecialNeeds addObject:draft]; 
    } 
    [tblVSpecialNeeds reloadData]; 


} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row > [self.specialNeedsList count]-1) { 
     [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
     return; 
    } 
    [self tableView:tblVSpecialNeeds accessoryButtonTappedForRowWithIndexPath:indexPath]; 

} 
+0

你好,我非常失落。有没有什么办法可以通过Skype与你交谈,也许你可以帮我解决这个问题?让我知道,谢谢! – dotyy

+0

从哪里可以了解如何从UITextField创建数组? – dotyy

+0

这是我的skype id imrahulvyas –

要删除刷卡,使用泰伯维的委托方法:

  • (无效)的tableView:(UITableView的*)的tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

    [arrAddData removeObjectAtIndex:indexPath.row];

    [tbldata reloadData];