私は、押されたときに、セルを強調表示し、強調表示を解除するために、tableViewの右隅にUIBarButtonItemを作成しようとしています。UITableViewCellを取得するには、UIBarButtonItemをタップして背景色を変更しますか?
言い換えれば、ユーザーがボタンを押すと、セルの範囲が背景色を白から黄色に変更します。
私はそのボタンを押すたびにアプリがクラッシュするので、私はそれを作ることに失敗しています。
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *barButton;
barButton = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"high.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(colorCells:)] autorelease];
self.navigationItem.rightBarButtonItem = barButton;
}
そしてここに、それはセルの範囲ハイライトにするために:私は失敗しています
- (void) colorCells:(id)sender
{
UITableViewCell *cell;
NSString *cellValue = cell.textLabel.text;
if ([cellValue isEqual: @"textTheCellShouldBeEqualTo"]){
cell.backgroundColor = [UIColor colorWithRed:251/255.0f green:255/255.0f blue:192/255.0f alpha:1]; ;
cell.imageView.image = [UIImage imageNamed:@"hot.png"];
}
else {
cell.imageView.image = nil;
cell.backgroundColor = [UIColor whiteColor];
}
}
を
ここで私は、ボタンを作成するために使用しているコードですか?それはうまく動作するはずです。または?何か不足していますか?ビューはUITableViewControllerです。
EDIT
私はそうのように私のコードを変更:
- (void) colorCells:(id)sender{
UITableViewCell *cell;
NSInteger nSections = [self.tableView numberOfSections];
for (int j=0; j<nSections; j++) {
NSInteger nRows = [self.tableView numberOfRowsInSection:j];
for (int i=0; i<nRows; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:j];
cell = [self.tableView cellForRowAtIndexPath:indexPath];
}
NSString *cellValue = cell.textLabel.text;
if ([cellValue isEqual: @"textTheCellShouldBeEqualTo"]){
cell.backgroundColor = [UIColor colorWithRed:251/255.0f green:255/255.0f blue:192/255.0f alpha:1]; ;
cell.imageView.image = [UIImage imageNamed:@"hot.png"];
}
else {
cell.imageView.image = nil;
cell.backgroundColor = [UIColor whiteColor];
}
[self.tableView reloadData];
}
}
今ではクラッシュしないが、それはあまりにも背景に色をしません。何か案は?
[self.tableView beginUpdates];を追加してください。および[self.tableView endUpdates];最後に – Novarg
まだ選択されたセルを色付けできません。 :S – Phillip