滚动时在UITableview单元格中保持自定义按钮的状态 [英] Maintain state of custom button in UITableview cell when scrolls
问题描述
我知道这个问题与其他问题非常相似,但我没有使用这种方法解决问题。我遵循大部分可用的解决方案,但它对我不起作用..
我知道当tableview滚动它重用单元格但我不知道维持按钮状态的方法。我将尝试使用以下链接
-
< a href =https://stackoverflow.com/questions/9423152/ios-maintaining-button-state-in-uitableviewcell> IOS:维护uitableviewcell中的按钮状态
我完成了所有的东西。使用标签,使用触摸点,但似乎没有什么似乎对我有用。所以帮助我..这是我的示例代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell =(subcategoryCell *)[tableView dequeueReusableCellWithIdentifier :NSStringFromClass([subcategoryCell class])forIndexPath:indexPath];
/ *
*设置按钮以便单元格订阅它
* /
cell.btnsubscribe =(UIButton *)[cell.contentView viewWithTag :303];
cell.btnsubscribe.tag = indexPath.row;
[cell.btnsubscribe addTarget:self action:@selector(clickBtnSubscribe :) forControlEvents:UIControlEventTouchUpInside];
if(![_ arraybtnState containsObject:listid])
{
[cell.btnsubscribe setImage:[UIImage imageNamed:@follow] forState:UIControlStateNormal];
[cell.btnsubscribe setSelected:YES];
}
else {
[cell.btnsubscribe setImage:[UIImage imageNamed:@follow] forState:UIControlStateNormal];
[cell.btnsubscribe setSelected:NO];
}
返回单元格;
}
_arrbtnstate包含用户遵循的ID。 br>
和listid包含来自数据库的唯一ID
并且点击事件方法......
- (IBAction)clickBtnSubscribe:(id)sender {
UIButton * button =(UIButton *)sender;
NSLog(@选择按钮标签%li,(长)button.tag);
NSNumber * tagnum = [NSNumber numberWithLong:(long)button.tag];
if(button.selected){
[self.arraybtnState addObject:tagnum];
[button setImage:[UIImage imageNamed:@after] forState:UIControlStateNormal];
NSLog(@订阅);
[self subscribeButton:button.tag];
[button setSelected:NO];
}
else
{
[self.arraybtnState removeObject:tagnum];
[button setImage:[UIImage imageNamed:@follow] forState:UIControlStateNormal];
NSLog(@取消订阅);
[self unsubscribeButton:button.tag];
[button setSelected:YES];
}
}
注意:在此代码中,按钮是在storyboard中创建的,但我也会在没有故事板的情况下尝试。
首先这一行 cell.btnsubscribe =(UIButton *)[cell.contentView viewWithTag:303];
将被更改。按照以下步骤在自定义 subcategoryCell
connect
到
btnsubscribe
。
UIButton
,这里是 btnsubscribe
。如果您发现很难在<$ c $中这个 c> cellForRowAtIndexPath:删除以下行
[cell.btnsubscribe setImage:[UIImage imageNamed:@按照] forState:UIControlStateNormal];
[cell.btnsubscribe setImage:[UIImage imageNamed:@follow] forState:UIControlStateNormal];
cell.btnsubscribe =(UIButton *)[cell.contentView viewWithTag:303];
更新以下行
if(![_ arraybtnState containsObject:listid])
{
[cell.btnsubscribe setSelected:YES];
}
else {
[cell.btnsubscribe setSelected:NO];
}
或只是 [cell.btnsubscribe setSelected :(![_ arraybtnState containsObject:listid])];
-
在行方法的单元格中保留
addTarget
(因为你有自定义单元格类,最好将按钮操作移动到单元格类并传递通过回调或委托来查看viewcontroller的结果。现在不讨论但是推荐)并通过删除
<$来更新clickBtnSubscribe:
p $ p>[button setImage:[UIImage imageNamed:@after] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@follow] forState:UIControlStateNormal];
这些行。假设其他部分正常工作。
I know this question is very similar to other questions, but i doesn't resolve the problem using that approach.i follow most of the available solution but it doesn't work for me.. I know that when tableview scrolls it reuse cell but i doesn't know the approach of maintaining the button state . i'll try with following link
I done all the things.use tags, use touch points and all but nothing seems to work for me.so help me out..and here is my sample code
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell = (subcategoryCell *)[tableView dequeueReusableCellWithIdentifier:NSStringFromClass([subcategoryCell class]) forIndexPath:indexPath];
/*
* Set button for cell to subscribe with it
*/
cell.btnsubscribe = (UIButton *)[cell.contentView viewWithTag:303];
cell.btnsubscribe.tag = indexPath.row ;
[cell.btnsubscribe addTarget:self action:@selector(clickBtnSubscribe:) forControlEvents:UIControlEventTouchUpInside];
if (![_arraybtnState containsObject:listid] )
{
[cell.btnsubscribe setImage:[UIImage imageNamed:@"follow"] forState:UIControlStateNormal];
[cell.btnsubscribe setSelected:YES];
}
else {
[cell.btnsubscribe setImage:[UIImage imageNamed:@"following"] forState:UIControlStateNormal];
[cell.btnsubscribe setSelected:NO];
}
return cell;
}
_arrbtnstate contains the ids that user follows.
and listid contains unique id comes from database too
And the click event method...
- (IBAction)clickBtnSubscribe:(id)sender {
UIButton *button = (UIButton*)sender;
NSLog(@"selected button tag %li", (long)button.tag);
NSNumber *tagnum = [NSNumber numberWithLong:(long)button.tag];
if (button.selected) {
[self.arraybtnState addObject:tagnum];
[button setImage:[UIImage imageNamed:@"following"] forState:UIControlStateNormal];
NSLog(@"Subscribe");
[self subscribeButton:button.tag];
[button setSelected:NO];
}
else
{
[self.arraybtnState removeObject:tagnum];
[button setImage:[UIImage imageNamed:@"follow"] forState:UIControlStateNormal];
NSLog(@"unsubscribe");
[self unsubscribeButton:button.tag];
[button setSelected:YES];
}
}
Note: In this code, button is created in storyboard.,but i'll also tried without storyboard too.
First of all this line cell.btnsubscribe = (UIButton *)[cell.contentView viewWithTag:303];
is to be changed. Follow the below steps and try
- in custom
subcategoryCell
connectIBOutlet
tobtnsubscribe
. - in storyboard, you can set both selected and not normal images to a
UIButton
, here it isbtnsubscribe
. If you find it difficult follow this in
cellForRowAtIndexPath:
remove below lines[cell.btnsubscribe setImage:[UIImage imageNamed:@"follow"] forState:UIControlStateNormal]; [cell.btnsubscribe setImage:[UIImage imageNamed:@"following"] forState:UIControlStateNormal]; cell.btnsubscribe = (UIButton *)[cell.contentView viewWithTag:303];
update the following lines
if (![_arraybtnState containsObject:listid] ) { [cell.btnsubscribe setSelected:YES]; } else { [cell.btnsubscribe setSelected:NO]; }
or simply [cell.btnsubscribe setSelected:(![_arraybtnState containsObject:listid] )];
Keep the
addTarget
in cell for row method (since you have custom cell class, it is better to move the button action to cell class and pass the result to viewcontroller by callback or delegate. not discussing that now but recomments) and update theclickBtnSubscribe:
by removing[button setImage:[UIImage imageNamed:@"following"] forState:UIControlStateNormal]; [button setImage:[UIImage imageNamed:@"follow"] forState:UIControlStateNormal];
these lines. assumes other parts are working properly.
这篇关于滚动时在UITableview单元格中保持自定义按钮的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!