0

I am becoming increasingly frustrated as I can not figure out how to get this to work. I have searched and searched and nothing seems to be helping me out to get this to do what I want.

What I am trying accomplish: I am trying to get the Task that the user will type in using the UITextField on top to add a new cell with their input as the text and with a check box to the left of it. When the cell is tapped, I would like to have the check box change to one with a check mark within it and for the entire cell to change opacity to 50%. Also, I would like to make the cell pop up with a delete button when it is swiped from left to right.

How would I make this all happen? I currently just have the UITextField added to the .xib using the Attribute Inspector to make it look the way it does. I also have a tool bar up top that I have used the Attributes Inspector to modify as well.

Nothing is coded to connect these to actions, they are just sitting in the .xib.

Here is an image of the rendering I created. (What I am trying to get)

https://i.stack.imgur.com/wabct.png

Here is what I have so far in Xcode in the .xib.

https://i.stack.imgur.com/emYXI.png

Thank you in advanced, Jacob

dotyy
  • 1
  • follow this tutorial http://adeem.me/blog/2009/05/30/iphone-sdk-tutorial-part-6-creating-custom-uitableviewcell-using-interface-builder-uitableview/ – Rahul Vyas May 28 '13 at 01:57

2 Answers2

0

You need to either add button to cell programatically or create a custom cell. When user taps on cell or checkbox you can change button's image to selected checkbox. for proper display of checkbox take a NSMutableArray and use the below code.

Note

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.

Follow this tutorial Get row for button tapped as well along with my code. You will get idea for accomplish this. Whatever I'm doing on - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath you need to do this on UIButton's action.

- (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];

}
Community
  • 1
  • 1
Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256
0

To delete on swipe, use Tableview's Delegate method:

  • (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

    [arrAddData removeObjectAtIndex:indexPath.row];

    [tbldata reloadData];

Mital
  • 241
  • 1
  • 5