开发者

problem with custom uitableviewcell

I have been playing around with tableviews for the past few days trying to get used to them, today I have been trying to create a custom uitableviewcell, I am working from the Table View Programming Guide in the apple documents and have this weird problem with something I am trying to do.

I have set up a custom cell nib file, it has two labels on it that both have tags (0 and 1) I have changed the File owners class to the nib that will be using it.

from there I have started programming the tableView:cellForRowAtIndexPath: method that displays my custom cells inside the tableview however as you will see, only my first label is showing when I assign it a string and I just don't know why...

Custom cell Nib

problem with custom uitableviewcell

tableView:cellForRowAtIndexPath: method

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
            [[NSBundle mainBundle] loadNibNamed:@"ManufactureSearchCell" owner:self options:nil];
            cell = vehicleSearchCell;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            self.vehicleSearchCell = nil;
        }
        // Configure the cell...
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        if(indexPath.section == 0)
        {
            if(indexPath.row == 0)
            {
                UILabel *label;
                label = (UILabel *)[cell开发者_开发知识库 viewWithTag:0];
                label.text = [[NSString alloc] initWithString:@"Manufacture :"];

                label = (UILabel *)[cell viewWithTag:1];
                label.text = [[NSString alloc] initWithString:@"empty"];
            }
            else if(indexPath.row == 1)
            {
//.....

Any help would be appreciated :)


Beware using viewWithTag and 0. That is the default tag for all views, so double check you are actually getting the label you want back from that instead of the cell's background view, or accessory view, or any other default view added to the cell. It can cause problems.

If you are reusing the cell, I'd recommend creating a custom UITableViewCell subclass and link them through IB. If you are not reusing that cell, and want it to display only once, definitely take up Karoly's suggestion to just have those labels as IBOutlets.


This strike me as odd- Why do you use

label.text = [[NSString alloc] initWithString:@"Manufacture :"];

as opposed to

label.text = @"Manufacture :";

The way it is, your not releasing your strings. using the @"xxx" short hand creates a string that is autoreleased. Not confident that this is the cause of your problem but screwy memory mgmt with strings can produce effects like your seeing. So cleaning it up would be a good start.

Also, make sure that in IB you made the cell have a reference to the file owners' vehicleSearchCell property. More of a stab in the dark.


I only see one thing in the code provided that sticks out to me. It looks like you are overwriting your label value. Which of the two is showing?

UILabel *label1;
label = (UILabel *)[cell viewWithTag:0];
label.text = [[NSString alloc] initWithString:@"Manufacture :"];

UILabel *label2
label2 = (UILabel *)[cell viewWithTag:1];
label2.text = [[NSString alloc] initWithString:@"empty"];

I would for simplicities sake just declare two labels. Or did you do that already? Is everything else working though? Are you programatically creating these labels? If not why don't you connect them through IB and give them unique names and access them that way? What is your CellViewStyle set to? It looks like you have the line that would set it to something commented out, is there any specific reason? You could also check your properties in the .xib file of your custom cell to see if there is something you need to change there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜