自定义tableview单元格

时间:2011-05-10 09:36:36

标签: iphone

我正在创建一个自定义的UItableviewcell。 我有标签和图像(选中标记)。点击该图像特定单元格被检查,我做了多次选择,但现在我想一次选择一个单元格。

这是我的代码:

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;



}

1 个答案:

答案 0 :(得分:3)

嗨朋友你必须像下面那样做一个复选标记..

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
if (curSelRowIndex == indexPath.row) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;}
else {
cell.accessoryType = UITableViewCellAccessoryNone;}
return cell;
}

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
curSelRowIndex = indexPath.row;
[tblHistory reloadData];
}