tableView的一些函数

郑哲彦
2023-12-01

//返回行数

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
    return [listData count];

}

//返回一个cell,即每一行所要显示的内容

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identify=@"identify";
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:identify];
    if(cell==nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identify];
    }
    NSUInteger row = [indexPath row];
    cell.textLabel.text = [listData objectAtIndex:row];
    UIImage *image=[UIImage imageNamed:@"qq.png"];
    cell.imageView.image=image;
    if(row<7)
        cell.detailTextLabel.text = @"Mr . Disney";
    else
        cell.detailTextLabel.text = @"Mr . Tolkien";
    return cell;

}

//设置一个叫indent level的属性,这样每一行都比上一行右移一点

-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
    return  row;

}

//设置点击事件前可以设置哪一行不能响应点击事件

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row=[indexPath row];
    if(row==0)
    {
        return nil;
    }
    return indexPath;

}

//设置点击事件

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
    NSString * rowvalue=[listData objectAtIndex:row];
    NSString * message=[[NSString alloc] initWithFormat:@"你选择了%@",rowvalue];
    UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"注意!" message:message delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil];
    [alert show];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

//设置列宽

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}


 类似资料: