【问题标题】:UITableView cell with custom & dymanic view - content具有自定义和动态视图的 UITableViewcell - 内容
【发布时间】:2014-02-20 08:15:27
【问题描述】:

我正在尝试在表格视图单元格中加载自定义视图。 确切地说,我有一个对象数组,我想使用自定义视图在单元格中动态加载内容。 我的对象结构是:

{  ID = 1;  date = "2014-01-06";  n1 = 12;  n2 = 3;  n3 = 34;  n4 = 5; n5 = 44;  n6 = 24;  report = "<null>";  win = 123443; }

我正在尝试在视图中显示 n1、n2 ... 的值,我采用了这种方法:

@interface MenuCell : UITableViewCell

@property (nonatomic, strong) BallView *ballView;
@property (nonatomic, strong) IBOutlet UILabel *labelDescription; 
@property (nonatomic, strong) IBOutlet UILabel *labelDetails;
@property (nonatomic, strong) IBOutlet UIView *ballsView;

+ (CGFloat)getCellHeight;

我的 BallView.h

@interface BallView : UIView

@property (nonatomic, strong) IBOutlet UIImageView *imgSelectedBall;
@property (nonatomic, strong) IBOutlet UILabel *labelNo;
@property (nonatomic, strong) IBOutlet UIActivityIndicatorView *spinner;

+ (BallView*)showInView:(UIView*)parrentView xOrigin:(NSInteger)xOriginValue;

@end

我的 BallView.m

+ (BallView*)showInView:(UIView*)parrentView xOrigin:(NSInteger)xOriginValue {
    NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"BallView" owner:self options:nil];
    BallView *modal = [nibViews objectAtIndex:0];
    modal.frame = CGRectMake(xOriginValue,ball_yOrigin, ball_width, ball_height);
    [modal setBackgroundColor:[UIColor greenColor]];
    [parrentView addSubview:modal];
    return modal; }

在 UITableView 委托方法中的 ViewController 中,我尝试以这种方式加载信息(我用 1 数值进行了测试)

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"NewsCell";
        MenuCell *cell = (MenuCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {
            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MenuCell_3" owner:self options:nil];
            for (id currentObject in topLevelObjects){
                if ([currentObject isKindOfClass:[UITableViewCell class]]){
                    cell =  (MenuCell *) currentObject;
                }
            }
        }

        __block TickteObj *obj   = [self.dataSourceArray objectAtIndex:indexPath.row];

        if (obj){        
            __block CGFloat leftMark = ball_offSet;
            for (int i = 1 ; i<5 ; i++){ //5 = my object has 6 numbers which must be displayed
                __block BallView  *ballView = [BallView showInView:cell.ballView xOrigin:leftMark];
                cell.ballView.labelNo.text = obj.n1;

                leftMark+=ball_offSet+ballView.frame.size.width;
            }
        }
        return cell;
    }

我需要帮助才能将值加载到我的单元格中。谢谢!

【问题讨论】:

  • 加载哪个值?什么不能与你所拥有的一起工作?或者问题出在其他 n 值上(显示TickteObj 的界面)。
  • 我尝试在第一次查看时显示来自 obj.no1 的值(i=0 来自 no1 的值,i=1 来自 obj.no2 的值 ...)。不指示使用if 语句,因为将来可能会增加视图的数量...

标签: ios iphone uitableview custom-controls


【解决方案1】:

你真的应该改变TickteObj提供的接口。它应该提供一个基于索引的方法,而不是提供许多属性(n1n2、...nx):

- (NSString *)nValueAtIndex:(NSInteger)index

并且可能在内部将 n 个值存储在一个数组中。这样,无论有多少值,您的循环都可以轻松工作。

【讨论】:

    猜你喜欢
    • 2013-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多