i have a button that i create in my CustomCell.
- (IBAction)onoffBtn:(id)sender
{
UIButton *button = (UIButton *)sender;
button.selected = !button.selected;
if (button.selected)
{
// Do action 1
}
else
{
// Do action 2
}
}
When i run it, the button is show in my cell inside UITableView that i create in TableViewController. I can press that onoffBtn inside my cell but it cant do action. How do i do so that my button can do action?
this is my cellForRowAtIndexPath inside my TableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:[CustomCell reuseIdentifier]];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell = _customCell;
_customCell = nil;
}
// Show my cell
return cell;
}
and for other question, where should i write for do process at? inside my TableViewController.m or my CustomCell.m? if you dont mind, please provide me with code and explanation, im new in here and still learning. i want to understand in every line of code that you write. Thank you.
Update:
This is my cellForRowAtIndexPath inside TableViewController.m so far:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:[CustomCell reuseIdentifier]];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell = _customCell;
_customCell = nil;
}
button.tag = indexPath.row;
cell.titleLabel.text = [titleArray objectAtIndex:indexPath.row];
cell.timerLabel.text = [timeArray objectAtIndex:indexPath.row];
time = [[secondArray objectAtIndex:indexPath.row] integerValue];
NSLog(@"Title %@, time %@, second %@, time %i, tag %d", titleArray, timeArray, secondArray, time, button.tag);
return cell;
}
and this is log said:
2012-08-04 17:38:36.257 Table[1269:f803] Title (
"Test 1",
"Test 2"
), time (
"10 secs",
"5 secs"
), second (
10,
5
), time 10, tag 0 // tag 0 for cell 1
2012-08-04 17:38:36.261 Table[1269:f803] Title (
"Test 1",
"Test 2"
), time (
"10 secs",
"5 secs"
), second (
10,
5
), time 5, tag 0 // tag should be 1 for cell 2 but it said 0. How to fix it?