[英]Scrolling of UITableview change the images of UIButton , UITableview scrolling issue
我正在做的是,我有一個UITableview
,我添加了UIButton
作為自定義視圖。 我給每個按鈕添加標簽,並在動作方法中收到標簽 。 當我按下按鈕時,它會更改所選和未選擇按鈕的圖像,但是當我滾動它時,它將進入正常狀態。
這是我在索引方法行的單元格
static NSString *CellIdentifier = @"Cell4";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [self tableviewCellWithReuseIdentifierFollowing:CellIdentifier];
}
followingButton = [UIButton buttonWithType:UIButtonTypeCustom];
[followingButton addTarget:self action:@selector(followingButtonpressed:)forControlEvents:UIControlEventTouchUpInside];
[followingButton setImage:[UIImage imageNamed:@"following12.png"] forState:UIControlStateNormal];
followingButton.frame = CGRectMake(220.0 ,20.0, 100, 40.0);
[cell.contentView addSubview:followingButton];
NSLog(@"row--%d",indexPath.row);
followingButton.tag=indexPath.row;
NSLog(@"followingButton.tag--%d",followingButton.tag);
[self configureCellFollowing:cell forIndexPath:indexPath];
return cell;
}
==================
//Here is the action method
-(void)followingButtonpressed:(id)sender
{
NSLog(@"sender tag --%d",[sender tag]);
UIButton *btnPly = (UIButton *)sender;
if([btnPly isSelected])
{
[btnPly setSelected:NO];
[btnPly setImage:[UIImage imageNamed:@"following12.png"] forState:UIControlStateNormal];
}
else
{
[btnPly setSelected:YES];
[btnPly setImage:[UIImage imageNamed:@"following_off12.png"] forState:UIControlStateNormal];
}
}
注意:此代碼為每行數據創建單元格(不重用單元格)
您只需按描述進行更改,可能對您有所幫助
NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
可能會解決你的問題:)
您的按鈕“重置”的原因是因為tableView:cellForRowAtIndexPath被多次調用(每當一個單元格即將可見時)。
每次調用此按鈕時,您都會重新初始化按鈕並將圖像重置為following12.png(默認狀態)。 這就是滾動按鈕時出現重置的原因。
您不能依賴單元本身來保留狀態,因為單元格每次都會重置。 您需要將狀態移動到其他位置(例如在數組實例變量中)。 然后,當您必須在tableView:cellForRowAtIndexPath中配置新單元格時,可以將其初始化為正確的狀態(基於數組)。
因此,創建一個名為myStateArray(或其他)的NSMutableArray實例變量來存儲您的按鈕狀態,然后您的cellForRowAtIndexPath看起來應該更像:
static NSString *CellIdentifier = @"Cell4";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [self tableviewCellWithReuseIdentifierFollowing:CellIdentifier];
}
followingButton = [UIButton buttonWithType:UIButtonTypeCustom];
[followingButton addTarget:self action:@selector(followingButtonpressed:)forControlEvents:UIControlEventTouchUpInside];
// -- Fetch the state from your array
BOOL buttonPressed = [[self.myStateArray objectAtIndex:indexPath.row] boolValue];
// -- Initialize the button state correctly
[followingButton setSelected:buttonPressed];
if (buttonPressed) {
[followingButton setImage:[UIImage imageNamed:@"following12.png"] forState:UIControlStateNormal];
} else {
[followingButton setImage:[UIImage imageNamed:@"following_off12.png"] forState:UIControlStateNormal];
}
followingButton.frame = CGRectMake(220.0 ,20.0, 100, 40.0);
[cell.contentView addSubview:followingButton];
NSLog(@"row--%d",indexPath.row);
followingButton.tag=indexPath.row;
NSLog(@"followingButton.tag--%d",followingButton.tag);
[self configureCellFollowing:cell forIndexPath:indexPath];
return cell;
}
然后按下按鈕確保保存狀態:
-(void)followingButtonpressed:(id)sender
{
// -- Save the state
[self.myStateArray insertObject:[NSNumber numberWithBool:[btnPly isSelected]] atIndex:[sender tag]];
NSLog(@"sender tag --%d",[sender tag]);
UIButton *btnPly = (UIButton *)sender;
if([btnPly isSelected])
{
[btnPly setSelected:NO];
[btnPly setImage:[UIImage imageNamed:@"following12.png"] forState:UIControlStateNormal];
}
else
{
[btnPly setSelected:YES];
[btnPly setImage:[UIImage imageNamed:@"following_off12.png"] forState:UIControlStateNormal];
}
}
據我所知,每當UITableView請求單元格時,您都會創建UIButton。 在這兩種情況下,如果您創建一個新單元格或使用出列單元格。 然后,每次在已創建的按鈕上創建並添加一個按鈕。 if
您的cellForRowAtIndexPath
方法應該如此, if
按鈕創建移動到單元格創建
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell4";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [self tableviewCellWithReuseIdentifierFollowing:CellIdentifier];
followingButton = [UIButton buttonWithType:UIButtonTypeCustom];
[followingButton addTarget:self action:@selector(followingButtonpressed:)forControlEvents:UIControlEventTouchUpInside];
[followingButton setImage:[UIImage imageNamed:@"following12.png"] forState:UIControlStateNormal];
followingButton.frame = CGRectMake(220.0 ,20.0, 100, 40.0);
[cell.contentView addSubview:followingButton];
}
NSLog(@"row--%d",indexPath.row);
followingButton.tag=indexPath.row;
NSLog(@"followingButton.tag--%d",followingButton.tag);
[self configureCellFollowing:cell forIndexPath:indexPath];
return cell;
}
但這不會成為您問題的競爭解決方案。 您可能知道UITableView使用“可重用”單元來減少系統內存使用量。 它只使用當前需要顯示的細胞數量。 因此,在具有100個單元格的表格中,它實際上將創建大約10個。並且它們都不會存儲所有按下/未按下按鈕的正確狀態。 要實現正確的行為,您必須拒絕使用標記並使用某些模型邏輯。 最簡單的方法 - NSMutableArray,您將存儲按鈕狀態。 在followingButtonpressed:
方法中,您將正確的對象設置為YES / NO,在cellForRowAtIndexPath
您將讀取此值。 檢查下面的代碼
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell4";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [self tableviewCellWithReuseIdentifierFollowing:CellIdentifier];
followingButton = [UIButton buttonWithType:UIButtonTypeCustom];
[followingButton addTarget:self action:@selector(followingButtonpressed:)forControlEvents:UIControlEventTouchUpInside];
[followingButton setImage:[UIImage imageNamed:@"following12.png"] forState:UIControlStateNormal];
followingButton.frame = CGRectMake(220.0 ,20.0, 100, 40.0);
[cell.contentView addSubview:followingButton];
BOOL isSelected = [[statesArray objectAtIndex:btnPly.tag] boolValue];
[self setState:isSelected forButton:followingButton];
}
NSLog(@"row--%d",indexPath.row);
followingButton.tag=indexPath.row;
NSLog(@"followingButton.tag--%d",followingButton.tag);
[self configureCellFollowing:cell forIndexPath:indexPath];
return cell;
}
-(void)followingButtonpressed:(id)sender
{
NSLog(@"sender tag --%d",[sender tag]);
UIButton *btnPly = (UIButton *)sender;
BOOL isSelected = [[statesArray objectAtIndex:btnPly.tag] boolValue];
[statesArray replaceObjectAtIndex:btnPly.tag withObject:[NSNumber numberWithBool:!isSelected]];
if(isSelected)
{
[self setState:NO forButton:btnPly];
}
else
{
[self setState:YES forButton:btnPly];
}
}
- (void) setState:(BOOL)state forButton:(UIButton *)button
{
if(state)
{
[button setSelected:NO];
[button setImage:[UIImage imageNamed:@"following12.png"] forState:UIControlStateNormal];
}
else
{
[button setSelected:YES];
[button setImage:[UIImage imageNamed:@"following_off12.png"] forState:UIControlStateNormal];
}
}
statesArray
在哪里
NSMutableArray *statesArray = [NSMutableArray new];
你必須在課堂上的某個地方創建並初始化它。 statesArray
的對象計數必須與tableView的單元格數相同。
這是因為每次滾動時都會調用此代碼
[followingButton setImage:[UIImage imageNamed:@"following12.png"] forState:UIControlStateNormal];
所以現在檢查
static NSString *CellIdentifier = @"Cell4";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [self tableviewCellWithReuseIdentifierFollowing:CellIdentifier];
followingButton = [UIButton buttonWithType:UIButtonTypeCustom];
[followingButton addTarget:self action:@selector(followingButtonpressed:)forControlEvents:UIControlEventTouchUpInside];
[followingButton setImage:[UIImage imageNamed:@"following12.png"] forState:UIControlStateNormal];
followingButton.frame = CGRectMake(220.0 ,20.0, 100, 40.0);
[cell.contentView addSubview:followingButton];
[self configureCellFollowing:cell forIndexPath:indexPath];
}
return cell;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.