user5955394
user5955394

Reputation:

Three state UIButton in ios TableView List

I have students attendance list in UITableView. Where each cell having an UIButton to set student attendance like Present, Absent and Leave on button click. How can I achieve this on single button.

Upvotes: 1

Views: 371

Answers (4)

Manish
Manish

Reputation: 315

You can do this by simply implementing the following @selector for your UIButton in UITableViewCell.

- (void)btnSetMode:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    if (btn.tag == 0)
    {
        [btn setTitle:@"Present" forState:UIControlStateNormal];
        btn.tag = 1;
        // Define your required task...
    }
    else if (btn.tag == 1)
    {
        [btn setTitle:@"Absent" forState:UIControlStateNormal];
        btn.tag = 2;
        // Define your required task...
    }
    else if (btn.tag == 2)
    {
        [btn setTitle:@"Leave" forState:UIControlStateNormal];
        btn.tag = 3;
        // Define your required task...
    }
    else if (btn.tag == 3)
    {
        [btn setTitle:@"Attendance" forState:UIControlStateNormal];
        btn.tag = 0;
        // Define your required task...
    }
}

Your single button will do all the required task as you wanted.

Upvotes: 2

Rohit Khandelwal
Rohit Khandelwal

Reputation: 1778

If you have An UIButton in your tableViewCell then add Target to your UIButton and set button Tag with indexPath.row in cellForRowAtIndexPath like-

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

    attendanceListTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];

    cell.studentNameLbl.text=[NSString stringWithFormat:@"Student %ld",(long)indexPath.row];

    cell.editBtn.tag=indexPath.row;

    [cell.editBtn setTitle:[attandanceArry objectAtIndex:indexPath.row] forState:UIControlStateNormal];

    [cell.editBtn addTarget:self action:@selector(editAttendance:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}
-(void)editAttendance:(id)sender{

    UIButton *btn=(UIButton*)sender;

    if ([btn.titleLabel.text isEqualToString:@"P"]) {
        [btn setTitle:@"A" forState:UIControlStateNormal];
        [attandanceArry replaceObjectAtIndex:btn.tag withObject:@"A"];
    }else if ([btn.titleLabel.text isEqualToString:@"A"]) {
        [btn setTitle:@"L" forState:UIControlStateNormal];
        [attandanceArry replaceObjectAtIndex:btn.tag withObject:@"L"];
    }else if ([btn.titleLabel.text isEqualToString:@"L"]) {
        [btn setTitle:@"P" forState:UIControlStateNormal];
        [attandanceArry replaceObjectAtIndex:btn.tag withObject:@"P"];
    }

}

In this attandanceArry is mutableArray having all default value is "P" with capacity of your number of students and editBtn is your cell UIButton. Here I use custom tableViewCell class i.e. attendanceListTableViewCell class having UIButton. At the end you can use attandanceArry as result array when you complete editing attendance. I hope this will help you...:)

Upvotes: 0

pbasdf
pbasdf

Reputation: 21536

Is a UISegmentedControl what you are looking for?

A UISegmentedControl

See the Apple Documentation here. It can be configured in a storyboard with the correct number of segments and segment titles. When the user selects a segment it will be highlighted, and any other segments are deselected.

Upvotes: 0

Tapani
Tapani

Reputation: 3231

Change the button title and background color when it's clicked. Personally, I would use three different buttons so that all different options would be visible at the same time. UIPicker is one option too.

Upvotes: 0

Related Questions