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.

- 31,729
- 16
- 153
- 201
-
Why don't you use three separate buttons? – Tapani Feb 22 '16 at 14:22
4 Answers
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.

- 306
- 4
- 11
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.

- 3,191
- 1
- 25
- 41
-
-
What you mean accoring to your UI? Can't you change the cell content to accommodate three buttons? And if you change the title and color you only need a single button. – Tapani Feb 23 '16 at 09:26
Is a UISegmentedControl
what you are looking for?
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.

- 21,386
- 4
- 43
- 75
-
I want to use single button having three state not segment Control. If i press button first time it show present, second time press show absent and third time press button show leave – Feb 23 '16 at 05:21
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...:)

- 1,778
- 15
- 23
-
Happy to help you :) . If this answer or any other one solved your issue, please mark it as accepted. – Rohit Khandelwal Feb 23 '16 at 07:36