繁体   English   中英

调用UITable View委托方法

[英]Calling UITable View Delegate Method

我正在使用UIViewController中的UITableView。我正在使用以下委托方法。

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

加载视图后,我发现所有代表都已被调用.....但是我需要在单击按钮时调用这些方法.....
我尝试过这种方式。

[self tableView:table numberOfRowsInSection:rows];
[self tableView:table cellForRowAtIndexPath:path];

我发现这些方法已被调用,但是什么也没发生(这些委托中的代码不起作用)...。我不知道为什么??? 有什么建议么???

在按钮单击功能内编写代码,它将调用tableview的所有委托

[self.tableView reloadData];

或完整的代码到您的解决方案是:

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


    static NSString *CellIdentifier = @"Cell";

    customCell *cell = [[customCell alloc]init];
    if (cell == nil) {
        cell = [[[customCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;


    NSArray *topLevelObjects=[[NSBundle mainBundle] loadNibNamed:@"customCell" owner:self options:nil];
    cell = (customCell *)[topLevelObjects objectAtIndex:0];


    int count=0;
    for(id currentObject in topLevelObjects)
    {
        count++;
        if([currentObject isKindOfClass:[customCell class]])
        {

            cell= (customCell *)currentObject;  

            UILabel *sName = (UILabel *)[cell.contentView viewWithTag:1]; 

            //Checking the flag that is set in the btnClicked event
            if (flag) {
                    sName.hidden=YES;
                }

                else{
                sName.text = [arrayResult objectAtIndex:indexPath.row];             
                }

        }
    }

    return cell;
    [topLevelObjects release];
    [cell release]; 

}

-(IBAction) btnClicked:(id) sender{
flag=YES;
[self.tableView reloadData];
}

试试这个,它将起作用..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM