为什么我们在UITableViewController中检查if(cell == nil)? [英] Why we are checking if (cell == nil) in UITableViewController?

查看:76
本文介绍了为什么我们在UITableViewController中检查if(cell == nil)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现基于UITableView的Application.For我选择UITableViewStyle是Group。在我的TableView中,它们是 15 部分,每个部分有1行。

I am trying to implement UITableView based Application.For that I select UITableViewStyle is Group.In my TableView their is 15 section each section having 1 row.

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

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

    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==12) 
    {
        return 120;
    }
    else
    {
        return 60;
    }

}

我想在Section上添加一个UITextView 12

I want add a UITextView on Section 12

为此我做了以下代码

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];



    }
    if ([indexPath section] == 12) 
    {
        if([indexPath row]==0)
        {
            descriptionTextField=[[UITextView alloc] initWithFrame:CGRectMake(5, 8, 290, 106)];
            descriptionTextField.font = [UIFont systemFontOfSize:15.0];   
            descriptionTextField.backgroundColor=[UIColor scrollViewTexturedBackgroundColor];

            [descriptionTextField setDelegate:self];
            [descriptionTextField setTag:2];
            [descriptionTextField setText:@"Enter Location Description."];

            descriptionTextField.keyboardType=UIKeyboardTypeDefault;
            descriptionTextField.returnKeyType=UIReturnKeyNext;


            descriptionTextField.textColor=[UIColor blackColor];
            descriptionTextField.editable=YES;
            descriptionTextField.autocapitalizationType=UITextAutocapitalizationTypeWords;
            descriptionTextField.autocorrectionType=UITextAutocorrectionTypeDefault;
            descriptionTextField.textAlignment=UITextAlignmentLeft;

            UIToolbar*  keboardToolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 32)];

            UIBarButtonItem *extra=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
            UIBarButtonItem *Done=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(keyboardDoneButtonActin:)];
            [Done setWidth:65.0f];
            [keboardToolBar setItems:[[[NSArray alloc]initWithObjects:extra,Done, nil]autorelease] ];
            [extra release];
            [Done release];

            [keboardToolBar setTintColor:[UIColor blackColor]];
            [keboardToolBar setAlpha:.70];
            [descriptionTextField setInputAccessoryView:keboardToolBar];
            [descriptionTextField setTag:101];
            [cell.contentView addSubview:descriptionTextField];
            [descriptionTextField release];
        }
    }

    return cell;
}

在initil阶段,这样的表视图

In the initil stage the table view like this

如果我上下滚动tableview,则更改uitextView部分,它将显示多个位置。

if i am scrolling tableview up and down , then the uitextView section changed and it will display multiple location.

我无法理解我的错,为什么会这样?

I can't understand my fault, why this happend?

如果在if(cell == nil)一边实现上述代码

if am implementing the above code in side the if(cell==nil)

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    if ([indexPath section] == 12) 
    {
        if([indexPath row]==0)
        {
           **/* implemention  all code here*/**

             [cell.contentView addSubview:descriptionTextField];
            [descriptionTextField release];
        }
    }

    return cell;

}

UITextView不显示,i认为它不是分配。

UITextView not disply, i think it is not allocating.

那么在
中实现的代码之间有什么区别if(cell == nil)
{

}里面

so what is the difference between code implementing in if (cell == nil) { inside }

if(cell == nil)
{
}
out side

if (cell == nil) { } out side

推荐答案

NSString *CellIdentifier = [NSString stringWithFormat:@"%i",indexPath.row];

// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

这可以用来代替

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

这是在单元格内写入的简单示例== nill

this is the simple example to write inside the cell ==nill

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

static NSString *CellIdentifier = @"CellIdentifier";
// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
cell= nil;
if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    [[cell.contentView viewWithTag:100+indexPath.row] removeFromSuperview]; 

    UIView *selectview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width, 30)];
    [selectview setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"strip_s12A-1_h.png"]]];
    cell.selectedBackgroundView = selectview;
    [selectview release];

    UILabel *cellTitle=[[UILabel alloc]initWithFrame:CGRectMake(40, 0, 300, 30)];
    //cellTitle.adjustsFontSizeToFitWidth=YES;
    [cellTitle setBackgroundColor:[UIColor clearColor]];
    [cellTitle setFont:[UIFont fontWithName:@"Arial Rounded MT Bold" size:17]];
    [cellTitle setTextColor:[UIColor blackColor]];
    cellTitle.tag = 100+indexPath.row;

        cellTitle.text= [[[cellArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row] valueForKey:@"Des2"];

    [cell.contentView addSubview:cellTitle];

    [cellTitle release];
}
return cell;
     }

我认为足够了

这篇关于为什么我们在UITableViewController中检查if(cell == nil)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆