开发者

Custom UITableViewCells with a programmatically created table

开发者 https://www.devze.com 2023-02-07 02:32 出处:网络
When Ever I try to load data from an array into my table using custom cells it only shows the last cell like its overwritin开发者_如何学编程g all the cells until I Get to the end of the list. The tabl

When Ever I try to load data from an array into my table using custom cells it only shows the last cell like its overwritin开发者_如何学编程g all the cells until I Get to the end of the list. The table was created programmatically and when the cells don't load the blank table scrolls. If however I load the cells then only the last cell shows up and it won't scroll.

I figure I made a typo or something but I can't really see anything that would be of much help. The parser works and the array is fine its just that the cells are acting strange. I wanted to make it so that I had a fixed bar over the table as well but first I needed to get the custom cells to show up in the table and scroll properly

schedule.h

@interface Schedule : UIViewController <UITableViewDelegate, UITableViewDataSource> {
    IBOutlet UITableView *newsTable;

    UIActivityIndicatorView *activityIndicator;

    CGSize cellSize;
    NSXMLParser *fileParser;

    NSMutableArray * events;


    NSMutableArray * announcements;

    NSMutableDictionary * item;

    NSString *currentElement;

    NSMutableString * currentTitle, * currentDate, * currentSummary, * currentId, * curr    entTime, *currentContent; 

    UITableViewCell *appCell;
}

@property(nonatomic, retain)IBOutlet UITableView *newsTable;
@property(nonatomic, retain)IBOutlet UITableViewCell *appCell;

-(void)parseXMLFileAtURL:(NSString *)URL;

@end

schedule.m

- (void)loadView{

    UITableView *tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]
                                                      style:UITableViewStylePlain];
    tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;

    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.tag = 4;
    [tableView reloadData];

    self.view = tableView;
    [tableView release];

}

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

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section        {
    return [events count];
}

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

    static NSString *CellIdentifier = @"MyCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = appCell;
        self.appCell = nil;
    }

    // Configure the cell.
    UILabel *label;
    label = (UILabel *)[cell viewWithTag:1];
    int eventIndex = [indexPath indexAtPosition: [indexPath length] - 1];
    label.text = [[events objectAtIndex: eventIndex] objectForKey: @"event"];

    return cell;
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {

    NSLog(@"webpage parsed");
    NSLog(@"events array has %d items", [events count]);
    newsTable = (UITableView*)[self.view viewWithTag:4];
    [newsTable reloadData];
}

I added more code the problem may be somewhere else.


I think your problem is that the cell is being released. When you call self.appCell = nil;, the object will be released and the variable set to nil. Try changing cell = appCell; to cell = [[appCell retain] autorelease]; so that the cell is retained long enough for the table view to retain it again.

Also, you could change int eventIndex = [indexPath indexAtPosition: [indexPath length] - 1]; to int eventIndex = indexPath.row;. The row and section properties were added to NSIndexPath specifically for use in UITableView, and it is guaranteed that the index path passed to cellForRowAtIndexPath: will contain exactly two indices.


try with following code;


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier  = @"CellIdentifier";
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // CustomCell is the class for standardCell
    if (cell == nil) 
    {
        NSArray *objectList = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        for (id currentObject in objectList) {
            if([currentObject isKindOfClass:[UITableViewCell class]]){
                cell = (CustomCell*)currentObject;
                break;
            }
        }
        cell.label.text = [[events objectAtIndex: indexPath.row] objectForKey: @"event"];
    }
    return cell;
}

when you load the xib file with NSBundle, it will return an array of Bundle. Then you have to check it and then use it.

If you tried like;

NSString *memberName; // member variable

NSString *name = memberName;
memberName = nil;
// the member variable "memberName, giving the reference to name; 
//So if you set memberName as nil, name also become a nil value.

// You have to use like;
name = [memberName retain];
memberName = nil;

Try to learn more about iPhone development documents.

Click here for Documentation.
Click here for sample code.

I think it will help you.

0

精彩评论

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