0

im newbie IOS and using IB, im working UITableView in UIViewController, i have implement "UITableViewDelegate, UITableViewDataSource" in viewcontroller and setdelegate,datasource for uitableview but it dosen't work, i dont know,

please help me!

thank for your read this article.

code viewcontroller.h

@interface ViewController : UIViewController<UITableViewDelegate,
UITableViewDataSource> @property (nonatomic, retain) IBOutlet UITableView *tableView;

code viewcontroller.m

- (void)viewDidLoad { [tableView setDataSource:self];
    [tableView setDelegate:self];
    dispatch_async(htvque, ^{
               NSData* data = [NSData dataWithContentsOfURL: listFilmByCate];
        NSError* error;
        jsonTable = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
        listDataTable = [jsonTable objectForKey:@"List"];
                dispatch_async(dispatch_get_main_queue(), ^{
                     [tableView reloadData];
        });
    });

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return listDataTable.count;
}

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

    NSDictionary *dataTable = [listDataTable objectAtIndex:indexPath.row];  
    NSData *receivedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[dataTable objectForKey:@"Thumbnail"]objectForKey:@"Url1"]]];    UIImage *image = [[[UIImage alloc] initWithData:receivedData] stretchableImageWithLeftCapWidth:50 topCapHeight:80];

    static NSString *simple_cell = @"simpleCell";

    CustomizeCell_Home *customize_cell = (CustomizeCell_Home *)[tableView dequeueReusableCellWithIdentifier:simple_cell];
    if (simple_cell == nil) 
    {}

    customize_cell.imageView.image = image;
    customize_cell.lbldescription.text =[dataTable objectForKey:@"LongDescription"];
    customize_cell.lblTitle.text =  [dataTable objectForKey:@"VName"];
    customize_cell.lblTitle.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
    customize_cell.lbldescription.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
    customize_cell.lbldescription.numberOfLines=4;
    customize_cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgtblRight.png"]];
    return customize_cell;
}
4

3 に答える 3

1

このコード行に問題があります。

CustomizeCell_Home *customize_cell = (CustomizeCell_Home *)[tableView dequeueReusableCellWithIdentifier:simple_cell];
if (simple_cell == nil) 
{}

customize_cellもしそうならどうなるでしょうnilか?

あなたはインスタンス化しておらず、customize_cellそのため、これらのメソッドを呼び出すことはできません。

customize_cell.imageView.image = image;
customize_cell.lbldescription.text =[dataTable objectForKey:@"LongDescription"];
customize_cell.lblTitle.text =  [dataTable objectForKey:@"VName"];
customize_cell.lblTitle.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
customize_cell.lbldescription.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
customize_cell.lbldescription.numberOfLines=4;
customize_cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgtblRight.png"]];
return customize_cell;

次のように、if内に単純な初期化セルを追加するだけです。

if (customize_cell == nil) 
{
  customize_cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"AnIdentifierString"] autorelease];
}

編集

これを変える:

CustomizeCell_Home *customize_cell = (CustomizeCell_Home *)[tableView dequeueReusableCellWithIdentifier:simple_cell];
if (simple_cell == nil) 
{}

customize_cell.lbldescription.text =[dataTable objectForKey:@"LongDescription"];
customize_cell.lblTitle.text =  [dataTable objectForKey:@"VName"];
customize_cell.lblTitle.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
customize_cell.lbldescription.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
customize_cell.lbldescription.numberOfLines=4;
customize_cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgtblRight.png"]];
return customize_cell;

に:

CustomizeCell_Home *customize_cell = (CustomizeCell_Home *)[tableView dequeueReusableCellWithIdentifier:simple_cell];
if (customize_cell == nil) 
{
  customize_cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"AnIdentifierString"] autorelease];
}

customize_cell.lbldescription.text =[dataTable objectForKey:@"LongDescription"];
customize_cell.lblTitle.text =  [dataTable objectForKey:@"VName"];
customize_cell.lblTitle.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
customize_cell.lbldescription.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
customize_cell.lbldescription.numberOfLines=4;
customize_cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgtblRight.png"]];
return customize_cell;
于 2012-08-21T14:28:59.137 に答える
0

セットする、

tableView。dataSource = self;
tableView。デリゲート=自己;

@requiredとして指示されたプロトコルを実装することは必須と言うことを意味します

-(UITableViewCell*) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath 
{
    // dint concentrate on this part, hope u are right
    NSDictionary *dataTable = [listDataTable objectAtIndex:indexPath.row];  
    NSData *receivedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[dataTable objectForKey:@"Thumbnail"]objectForKey:@"Url1"]]];    
    UIImage *image = [[[UIImage alloc] initWithData:receivedData] stretchableImageWithLeftCapWidth:50 topCapHeight:80];

    static NSString *simple_cell = @"simpleCell";
    // works like double ended queue.... cells are re-used when available... so when cell is nil, u need to create
    CustomizeCell_Home *customize_cell = (CustomizeCell_Home *)[tableView dequeueReusableCellWithIdentifier:simple_cell];  
    if (simple_cell == nil) 
    {
         // CustomizeCell_Home is linked to CustomizeCell_Home_Reference using nib,
         [[NSBundle mainBundle] loadNibNamed: @"CustomizeCell_Home" owner: self options: nil];

         customize_cell = CustomizeCell_Home_Reference;
    }
    .....  
    .....  
    return customize_cell;

}

于 2012-08-21T14:44:39.960 に答える
-1

まず、セルが割り当てられることはありません。if(simple_cell==nil) を実行する代わりに、if(customize_cell==nil) を実行し、そこで初期化コードを実行する必要があります。それ以外の場合は、作成したばかりの文字列を比較し、値を nil に対して設定しています。

于 2012-08-21T14:25:54.363 に答える