我之前看過一些帖子,但沒有得到答案,這就是爲什麼我想以更有效的方式再次發帖。我如何在下圖中使用UITableView
中的check-uncheck功能。選中取消選中uitableview單元格中的按鈕
這是我想要的表格,當我點擊任何單元格的按鈕時,按鈕圖像將會改變,而不是所有的單元格。
我之前看過一些帖子,但沒有得到答案,這就是爲什麼我想以更有效的方式再次發帖。我如何在下圖中使用UITableView
中的check-uncheck功能。選中取消選中uitableview單元格中的按鈕
這是我想要的表格,當我點擊任何單元格的按鈕時,按鈕圖像將會改變,而不是所有的單元格。
做檢查,取消選中的功能僅buttonClicked:
方法是不夠的。您還將cellForRowAtIndexPath:
方法中的條件設置爲,其中或其中未選中,因爲cellForRowAtIndexPath:
方法會每次調用時將滾動您的UITableView
並更新單元格。 我看到你以前的問題,你添加兩個按鈕與兩個動作不是一個好方法,只需更改按鈕的圖像check-uncheck
。
所以這是我這個做 - 在ViewController.m
類現在
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
{
IBOutlet UITableView *tblView;
NSMutableArray *arrayCheckUnchek; // Will handle which button is selected or which is unselected
NSMutableArray *cellDataArray; // this is your data array
}
@end
-
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
arrayCheckUnchek = [[NSMutableArray alloc]init];
//Assign your cell data array
cellDataArray = [[NSMutableArray alloc]initWithObjects:@"cell-1",@"cell-2",@"cell-3",@"cell-4",@"cell-5", nil];
// setting all unchecks initially
for(int i=0; i<[cellDataArray count]; i++)
{
[arrayCheckUnchek addObject:@"Uncheck"];
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [cellDataArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [cellDataArray objectAtIndex:indexPath.row];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(270.0, 7.0, 30.0, 30.0)];
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
[button setImage:[UIImage imageNamed:@"uncheck_icon"] forState:UIControlStateNormal];
else
[button setImage:[UIImage imageNamed:@"check_icon"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
return cell;
}
-(void)buttonClicked:(id)sender
{
//Getting the indexPath of cell of clicked button
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:tblView];
NSIndexPath *indexPath = [tblView indexPathForRowAtPoint:touchPoint];
// No need to use tag sender will keep the reference of clicked button
UIButton *button = (UIButton *)sender;
//Checking the condition button is checked or unchecked.
//accordingly replace the array object and change the button image
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
{
[button setImage:[UIImage imageNamed:@"check_icon"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Check"];
}
else
{
[button setImage:[UIImage imageNamed:@"uncheck_icon"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Uncheck"];
}
}
最後它看起來就像 -
標籤與定製的UITableViewCell在IB設計非常有用。如果您以編程方式創建單元格,則不需要標籤。您只能使用它們來查找正確的uiview來設置屬性。
我正在編程,我需要做的確切。 –
請勿爲此使用標籤。只需在tableView:didSelectRowAtIndexPath中進行所有必需的操作: –
好吧,我會嘗試這一點。 –
是的,這很容易設置標籤,同時在UITableViewCell中聲明類並使用標籤標識它們。我已經發布了一些示例代碼供您參考。我不確定它會解決你的問題。你問了如何設置標籤並使用標籤來識別這些類。所以它會給你一些關於你的問題的基本想法。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
label1 = [[UIView alloc] initWithFrame:CGRectMake(2, 20, 15, 15)];
label1.tag = 100;
label1.backgroundColor = [UIColor whiteColor];
label1.layer.cornerRadius = 5;
[cell.contentView addSubview: label1];
label2 = [[UILabel alloc] initWithFrame:CGRectMake(25, 2, 165, 23)];
label2.tag = 101;
label2.font = [UIFont boldSystemFontOfSize:15];
label2.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview: label2];
label3 = [[UILabel alloc] initWithFrame:CGRectMake(190, 2, 90, 23)];
label3.tag = 102;
label3.font = [UIFont systemFontOfSize:14];
label3.textColor = [UIColor colorWithRed:0.14117670588235 green:0.435294117647059 blue:0.847058823529412 alpha:1];
label3.backgroundColor = [UIColor clearColor];
label3.textAlignment = UITextAlignmentRight;
[cell.contentView addSubview: label3];
}
label1 = (UILabel *) [cell.contentView viewWithTag:100];
NSString *string1 = [array1 objectAtIndex:indexPath.row];
label2 = (UILabel *) [cell.contentView viewWithTag:101];
NSString * string2 = [array2 objectAtIndex:indexPath.row];
label3 = (UILabel *) [cell.contentView viewWithTag:102];
NSString * string3 = [array3 objectAtIndex:indexPath.row];
return cell;
}
請讓我知道這是否有用。否則,我們會採取其他方式。
快樂編碼。謝謝。
讓我檢查一下,我會讓你知道的。 –
這不是我所需要的,請看看我修改後的問題。 –
我同意@VakulSaini因爲ü可以做到這一點來處理這cell觸控或滑動或什麼都,這是一個特別吸引人的UITableView
-(void)handleSwipLeft:(UISwipeGestureRecognizer *)gestureRecognizer
{
CGPoint Location = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:Location];
cell = [tableView cellForRowAtIndexPath:indexPath];
}
請檢查您的答案。 –
使用cellForRowAtIndexPath
方法和這種方法添加任何組件一樣(UITextField
,UILabel
.....等)在您的UITableViewCell
通過使用[cell.contentView addSubview:YourComponentName];
並給每個組件標籤由indexPath.row
。
諸如此類,,,
YourComponentName.tag = indexPath.row;
你這是什麼想做請問,因爲我認爲可以有更好的方法來解決喲你的問題,而不是標籤。 – TheTiger
請參閱此鏈接(http://stackoverflow.com/questions/10630755/array-displaying-repeated-object-in-table-view-cell/10631393#10631393)。 –
在'UITableViewCell'上設置標籤是一個壞方法。 – TheTiger