0

I wanna create the uitableview like this image . loading data from server and assigning the values to columns of Row. I saw the link of stack, but not helpful to me. UPDATE My code: -

#pragma mark UITableViewDelegate methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
    return [modelArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = nil;
    static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
    cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleGray;      
    // Configure the cell...
    RankModel *model = [modelArray objectAtIndex:indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@    %@     %@     %@     %@     %@",  model.level, model.name, model.score, model.rightAnswersCount, model.currentRank, model.country];
    return cell;
}

but I wanna display like the given image. so please help me to overcome this problem. thanks in advance.

Community
  • 1
  • 1
Lion
  • 872
  • 1
  • 17
  • 43

1 Answers1

2

Well this will require a bit more code than the method you present. My suggestion is you could create a UILabel for each of your fields, instrad of using a single NSString. Don't use cell.textLabel, but rather add your content on cell.contentView and then you can manage each label's color, background color and labels' sizes. The "grid" look can be rendred by assigning a white color to contentView and assigning a green color for example to each label's background. For example, after your cell is created :

cell.contentView.backgroundColor = [UIColor clearColor];

UILabel* aLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 0.0, 100, 44)];
aLabel.tag = indexPath.row;
aLabel.textAlignment = UITextAlignmentLeft;
aLabel.textColor = [UIColor whiteColor];
aLabel.text = @"Test";
aLabel.backgroundColor = [UIColor greenColor];
[cell.contentView addSubview:aLabel];
[aLabel release];

Start your next label at 201 or more to leave an impression of a white vertical line. Keep the row index in the tag so you can manage alternate colors in :

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell
                                                        *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == 0 || indexPath.row%2 == 0) {
        // use light green, get access to the labels via [cell.contentView viewWithTag:indexPath.row]
    } else {
        // use dark green   
    }
}

Hope this helps.

gregory
  • 826
  • 1
  • 6
  • 6