0
我正在創建一個自定義UItableviewcell。 我有標籤和圖像(複選標記)。點擊該圖片後,特定的單元格會被檢查,我做了多次選擇,但現在我想一次選擇一個單元格。自定義tableview單元格
這是我的代碼:
iphone
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
BOOL checkButtonPressed=FALSE;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UIImage *image = (checkButtonPressed) ? [UIImage imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"];
UIButton *checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
checkButton.frame = frame; // match the button's size with the image size
[checkButton setBackgroundImage:image forState:UIControlStateNormal];
[checkButton addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
//cell.imageView.image = [UIImage imageNamed:@"unchecked.png"];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
for (int ii=0; ii<=[checkedArr count]; ii++) {
cell.textLabel.text =[dataArray objectAtIndex:ii];
}
//cell.backgroundColor = [UIColor clearColor];
cell.accessoryView = checkButton;
[cellArr addObject:cell];
[checkedArr addObject:[NSNumber numberWithBool:FALSE]];
return cell;
}
- (void)checkButtonTapped:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:tableView];
NSIndexPath *indexPath = [tableView indexPathForRowAtPoint: currentTouchPosition];
NSLog(@"val %d",[checkedArr count]);
if (indexPath != nil && iChecked == 0)
{
[self tableView:tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"in accessoryButtonTappedForRowWithIndexPath %d",indexPath.row);
BOOL checked = [[checkedArr objectAtIndex:indexPath.row] boolValue];
[checkedArr replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:!checked]];
UITableViewCell *cell = [cellArr objectAtIndex:indexPath.row];
UIButton *button = (UIButton *)cell.accessoryView;
UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
cell.accessoryView = button;
iChecked = 0;
}
嗯,我第二上述評論。 – 2011-05-10 09:40:33
同意,現在不可能得到任何幫助...... – Joetjah 2011-05-10 09:48:07