【问题标题】:change static UITableViewCell background color inside a UIPopoverController xcode5更改 UIPopoverController xcode5 内的静态 UITableViewCell 背景颜色
【发布时间】:2014-07-15 10:19:49
【问题描述】:

我正在 Xcode 5 中为 iOS7.1 开发应用程序

我想将最后一行的表格视图单元格的背景颜色更改为如下所示

但是当我在 UIPopoverController 中调用这个 UITableViewController 时,我得到了这个:

不知何故,我的 tableview 单元格失去了原来的背景颜色

这是我的代码:

//iPad Popover Section
        if (!resultBTPopover || !resultBTPopover.popoverVisible)
        {
            ResultadosBalanceTransferVC *controller   = [self.storyboard instantiateViewControllerWithIdentifier:@"ResultadosBalanceTransferVC"];
            //controller.delegate             = self;
            controller.title                = @"Resultados";
            controller.amountValue          = self.amountTextfield.text;
            controller.promoRateValue       = self.promoRateTextfield.text;
            controller.normalRateValue      = self.normalRateTextfield.text;
            controller.otherBankRateValue   = self.otherBankRateTextfield.text;
            controller.termValue            = self.termTextfield.text;



            navController                  = [[UINavigationController alloc]initWithRootViewController:controller];
            navController.toolbarHidden     = FALSE;

            NSDictionary *textAtributesDictionaryTitle = [NSDictionary dictionaryWithObjectsAndKeys:
                                                          UIColorFromRGB(toolbarTintColor),  NSForegroundColorAttributeName,
                                                          [UIFont fontWithName:@"HelveticaNeue-BoldCond" size:19.0f], NSFontAttributeName,
                                                          nil];

            [navController.navigationBar setTitleTextAttributes:textAtributesDictionaryTitle];

            resultBTPopover   = [[UIPopoverController alloc] initWithContentViewController:navController];


            [resultBTPopover presentPopoverFromRect:CGRectMake(self.view.center.x - (self.view.center.x * .2734), self.view.center.y - (self.view.center.y * .6510), 300, 400)
                                             inView:self.view permittedArrowDirections:0
                                           animated:YES];
        }
        else{
            [resultBTPopover dismissPopoverAnimated:YES];
            resultBTPopover = nil;
        }
    }

如何获得我原来的 tableview 单元格颜色???

提前感谢您的支持


编辑:我会提出 Plokstorm 提出的方法

ResultsCell.h:

#import <UIKit/UIKit.h>

@interface ResultsCell : UITableViewCell

@property (nonatomic, weak) IBOutlet UILabel *label1;
@property (nonatomic, weak) IBOutlet UILabel *label2;
@property (nonatomic, weak) IBOutlet UILabel *label3;
@property (nonatomic, weak) IBOutlet UILabel *label4;
@property (nonatomic, weak) IBOutlet UILabel *label5;

@end

在故事板上我这样做了:

全部改成ResultsCell自定义类

将所有属性与 UITableViewCell 的可视标签相关联

在 CODE 上我这样做是为了测试:

ResultadosBalanceTransferVC.m

#import "ResultsCell.h"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ResultsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        cell = [[ResultsCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:@"Cell"];
    }

    if (indexPath.row == 2){
        cell.backgroundColor = [UIColor redColor];
        cell.label1.text = @"Pago Total";
    }

    return cell;

}

并且...仍然得到相同的结果:

【问题讨论】:

    标签: objective-c uitableview xcode5 uipopovercontroller ios7.1


    【解决方案1】:

    您需要在情节提要中使用它更改单元格类。

    如果你不创建单元类,你应该创建一个扩展 UITableViewCell 的对象。然后你可以添加它的属性。

    @interface ResultsCell : UITableViewCell
    @property (nonatomic, weak) IBOutlet UILabel *lYourFirstLabel;
    

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        ResultsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        if (cell == nil) {
            cell = [[ResultsCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:@"Cell"];
        }
        if (indexPath.row == 2)
              cell.backgroundColor = [UIColor redColor];
              [cell.lYourFirstLabel setText:@"hello"];
    
        return cell;
    }
    

    【讨论】:

    • 我按照你的建议做了,但我得到了相同的结果,我会用我所做的所有步骤更新我的评论,以了解我是否错过了什么
    【解决方案2】:

    试试看:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:@"Cell"];
        }
        if (indexPath.row == 2)
              cell.backgroundColor = [UIColor redColor];
    
    
        return cell;
    }
    

    【讨论】:

    • 完成,我添加了你的代码,它可以工作,但它也会删除我所有的标签,我会更新我的帖子以查看结果
    【解决方案3】:

    向我们展示你的

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    

    您可以使用cell.backgroundColor 更改单元格背景。

    【讨论】:

    • 我没有实现该方法,因为这是一个静态 tableview 控制器,我添加了它并收到此错误:*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“UITableView dataSource 必须从 tableView 返回一个单元格:cellForRowAtIndexPath:' 将更新我的代码以向您展示方法实现
    猜你喜欢
    • 1970-01-01
    • 2015-11-01
    • 2010-09-18
    • 1970-01-01
    • 2012-08-22
    • 2010-11-12
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    相关资源
    最近更新 更多