Tyrone Prude
Tyrone Prude

Reputation: 382

UITableView cell with custom & dymanic view - content

I'm trying to load a custom view in a tableview cell. Exactly, I have an array of objects and I want load the content dymanically in the cell using a custom view. My object structure is :

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

I'm trying to dispaly the values for n1, n2 ... in a view and I made this method :

@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;

My 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

My 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; }

In my ViewController in UITableView Delegate Methods I tried to load the information in this way (I made a test with the 1 number value) :

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

I need help to load the value in my cell. Thanks !

Upvotes: 2

Views: 95

Answers (1)

Wain
Wain

Reputation: 119031

You should really change the interface provided by TickteObj. Rather than providing a number of properties (n1, n2, ... nx), it should provide an index based method:

- (NSString *)nValueAtIndex:(NSInteger)index

And probably store the n values internally in an array. In this way it doesn't matter how many values there are and your loop can work easily.

Upvotes: 2

Related Questions