我有一个带有CustomCell的UITableView
,每个单元格都有一个UIButton
,而按钮正在 cellForRowAtIndexPath
cell.ffButton.tag = indexPath.row;
[cell.ffButton addTarget:self action:@selector(changeTitle:) forControlEvents:UIControlEventTouchUpInside];
我希望如果用户按下UIButton
,它只会更改该按钮标题。
操作 changeTitle 位于我的 UITableView.m
中,而不在 CustomCell.m
文件中
搜索谷歌搜索我需要的东西,但找不到任何东西。
任何想法? :)
更新:
我的tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"NotMutualCustomCell";
notMutualCustom *cell = (notMutualCustom *) [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.ffButton.hidden = NO;
cell.ffButton.tag = indexPath.row;
[cell.ffButton setTitle:@"Title" forState:UIControlStateNormal];
[cell.ffButton addTarget:self action:@selector(changeTitle:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
答案 0 :(得分:0)
您可以为按钮的不同状态指定不同的标题:
[button setTitle:@"ON" forState:UIControlStateNormal];
[button setTitle:@"OFF" forState:UIControlStateSelected];
接下来,您可以更改UIControlEventTouchUpInside
事件的操作处理程序中按钮的状态:
- (void)changeTitle:(UIButton *)button
{
button.state = button.state == UIControlStateNormal ? UIControlStateSelected : UIControlStateNormal;
//keep new state of the button in model
self.dictionary[[@(button.tag) stringValue]] = @(state);
}
您必须在应用程序的模型中存储按钮的状态。您可以为此目的使用字典(记住正确初始化字典的值 - 到正确的状态)。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//create cell
cell.button.state = [self.dictionary[[@(button.tag) stringValue]] integerValue];
return cell;
}
答案 1 :(得分:0)
自定义表格视图单元格
[btnAddFriend setTitle:@"Follow" forState:UIControlStateNormal];
[btnAddFriend setTitle:@"Unfollow" forState:UIControlStateSelected];
表格视图实施
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell"];
if (!cell) {
NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil];
cell = [nibs firstObject];
[cell.btnAddFriend addTarget:self action:@selector(followButtonTapped:forEvent:) forControlEvents:UIControlEventTouchUpInside];
}
return cell;
}
-(void)followButtonTapped:(id)sender forEvent:(UIEvent*)event{
//
UIButton *followButton = (UIButton*)sender;
followButton.selected = !followButton.selected;
//
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
// Lookup the index path of the cell whose checkbox was modified.
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
NSLog(@"index:%@",indexPath); // you can get the index of array from here
}