0

我有一个表格视图和里面的 2 个自定义单元格。我想使用 (X) 符号,即十字符号和勾号。我知道刻度线我们有 UITableViewCellAccessoryCheckmark 但如何处理 (X) 符号。我们可以为此提供任何自定义配件,否则如何实现?

我的代码:

#pragma mark - Table View Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    optionsLabelArray = [[NSArray alloc] initWithObjects:@"Yes", @"No", nil];

    static NSString *CellIdentifier = @"CheckerCell";
    CustomCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.optionLabel.text = [optionsLabelArray objectAtIndex:indexPath.row];
    cell.optionLabelSubtitle.text = [optionsLabelSubtitleArray objectAtIndex:indexPath.row];
    if(indexPath.row == 0)
    {
        cell.textLabel.textColor = [UIColor colorWithRed:78/255.0 green:157/255.0 blue:19/255.0 alpha:1.0];
    }
    else if(indexPath.row == 1)
    {
        cell.textLabel.textColor = [UIColor colorWithRed:167/255.0 green:19/255.0 blue:43/255.0 alpha:1.0];
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView ==_optionsTableView1) {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

        previousSelectedCell1.accessoryType = UITableViewCellAccessoryNone;
        cell.accessoryType=UITableViewCellAccessoryCheckmark;
        previousSelectedCell1 = cell;
        NSLog(@"Row : %ld", (long)indexPath.row);
        if(indexPath.row == 0)
        {
            self.weatherSafeToPlay = YES;
        }
        else
        {
           // MatchDayDataController *sharedController = [MatchDayDataController sharedDataController];
           // sharedController.actions = [sharedController.actions stringByAppendingString:@"Check Weather. "];
            //[sharedController.actions appendString:@"Check Weather. "];

            self.weatherSafeToPlay = NO;
        }
        NSLog(@"Is Weather safe: %hhd", self.weatherSafeToPlay);
    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
4

2 回答 2

2

没有听说过任何用于交叉的附件类型,但在这种情况下,我只使用 unicode 字符。
它比使用图像更轻,并且只需更改文本即可。

  • @"\u2713" 代表 ✓</li>
  • @"\u2717" 代表 ✗</li>

你可以:

  1. 创建一个自定义 UITableViewCell 具有 32by32(或任何尺寸)UILabel(在右侧/左侧/中间/任何地方)
  2. 在 -didSelectRowAtIndexPath 中,可以更改这个 UILabel 的文本
  3. @"\u2713" 用于复选标记或@"\u2717" 用于交叉标记

一个简单的例子(在默认的 UITableViewCell 的默认 textLabel 上,仅供参考):

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if([cell.textLabel.text isEqualToString:@"\u2713"]) {
        cell.textLabel.text = @"\u2717";
    }
    else {
        cell.textLabel.text = @"\u2713";
    }
}

或者...当不使用自定义单元格时,例如:

-(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];
    }

    //set your text to cell.textLabel.text

    //create your own UILabel
    UILabel *lblAcc = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 32, 32)];
    [lblAcc setTag:100];
    [lblAcc setText:@"?"];
    [lblAcc setBackgroundColor:[UIColor redColor]];
    [lblAcc setTextAlignment:NSTextAlignmentCenter];

    //set as custom AccessoryView on cell
    [cell setAccessoryView:lblAcc];

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *strAccessory = [(UILabel *) [cell viewWithTag:100] text];

    //the -isEqualToString is a bit heavier than checking a simple bool or int value
    //so since you should be remembering the selected cells anyways, you can change
    //the if-logic to compare with a bool rather than the string comparison.
    //but for now, we'll do:
    if([strAccessory isEqualToString:@"\u2713"]) {
        [(UILabel *) [cell viewWithTag:100] setText:@"\u2717"];
    }
    else {
        [(UILabel *) [cell viewWithTag:100] setText:@"\u2713"];
    }
}

有关更多 unicode 字符...此链接:
http ://en.wikibooks.org/wiki/Unicode/List_of_useful_symbols

于 2013-11-08T05:36:28.343 回答
1

您可以将自己的图像分配给表格单元格的附件视图。创建 (X) 符号的图像并将该图像用作附件视图。

cell.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Your X Image"]];
于 2013-11-08T05:31:46.313 回答