2013-03-14 72 views
0

我试过这么多的例子,但不行。请检查我的代码并给出解决方案。提前感谢。TableView数据在滚动时消失?

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

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier]; 
    } 
if (indexPath.section==0) 
{ 
    img=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    img.frame=CGRectMake(25, 15, 75, 75); 
    img.layer.cornerRadius=6.0; 
    img.clipsToBounds=YES; 
    img.layer.borderColor=[[UIColor grayColor]CGColor]; 
    [img setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal]; 
    [cell.contentView addSubview:img]; 


    [img addTarget:self action:@selector(takePic:) forControlEvents:UIControlEventTouchDown]; 
    img.titleLabel.textColor=[UIColor blackColor]; 
    text=[[UITextField alloc]initWithFrame:CGRectMake(105, 30, 200, 30)]; 
    [email protected]"Name"; 
    [text setBorderStyle:UITextBorderStyleRoundedRect]; 
    [cell addSubview:text]; 
    [cell addSubview:img]; 
    else if (indexPath.section==1) 
{ 
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(20, 15, 100, 20)]; 
    label.text=[dosageArray objectAtIndex:indexPath.row]; 
    label.backgroundColor=[UIColor clearColor]; 
    label.font=[UIFont boldSystemFontOfSize:15.0]; 
    label.textColor=[UIColor blackColor]; 
    [cell addSubview:label]; 
    return cell; 
    }} 

我在tableview.In第一部分我有一个文本框和ImageView的三节。如果我滚动文本字段和ImageView的消失,这是问题

+0

在深处解释...我不能清楚地得到它,wt是使用其他条件> ? – iPatel 2013-03-14 12:27:02

+0

删除else块。 – Balu 2013-03-14 12:27:48

+0

这个'if(cell == nil)'的用法是这样的,无论这是真还是假,你正在做同样的事情。你也只是创建一个单元格并返回它。你在哪里分配数据? – 2013-03-14 12:31:25

回答

0

试试这段代码将NSMutableArray添加到all.Here displayArray是NSMutableArray。

text.text=[displayArray objectAtIndex:1]; 
0

从别的部分&删除cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];离开它空白

欣赏节目!

+0

对不起还是同样的问题,我已经试过了..谢谢你的时间.. – 2013-03-14 12:28:56

+0

好吧,然后尝试使用此UITableViewCell * cell = nil; static NSString * CellIdentifier = @“Cell”; cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; cell = [[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; return cell; – 2013-03-14 12:32:18

+0

@ForamMukundShah这不会是一个问题。因为他总是在创建一个新的单元格 – 2013-03-14 12:32:24

0

试试这个:

static NSString *CellIdentifier = @"Cell"; 

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

cell.textLabel.text = @"Hello"; 

return cell; 

感谢。

0

试试这个

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

    static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier]; 

    } 

    cell.textLabel.text = @""// value from your datasource 

    return cell; 

    } 
-1

SOLUTION 1

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

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier]; 

//your code 

} 

解决方案2

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

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier]; 

} 

cell.textLabel.text= @"YOUR TEXT"; 

} 

希望这会帮助你。

所有最好的!

0

请尝试使用这一个...

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

      static NSString *CellIdentifier = @"Cell"; 
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
      if (cell == nil) 
      { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier]; 

      } 
      return cell; 
    } 
1

注:如果你有有限的数据,然后使用解决方案 - 1,因为这是不好的内存管理,其他明智解决方案 - 2对你有好处。 (解决方案 - 1可以是用于Ü有用)

SOLUTION - 1

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row]; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
    } 

     cell.textLabel.text = @"NameOFData "; 

    return cell; 
} 

SOLUTION - 2

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

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

     cell.textLabel.text = @"NameOFData "; 

    return cell; 
} 

EDITED:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row]; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if(cell == nil) 
     { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
      if (indexPath.section==0) 
      { 
    img=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    img.frame=CGRectMake(25, 15, 75, 75); 
    img.layer.cornerRadius=6.0; 
    img.clipsToBounds=YES; 
    img.layer.borderColor=[[UIColor grayColor]CGColor]; 
    [img setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal]; 
    [cell.contentView addSubview:img]; 


    [img addTarget:self action:@selector(takePic:) forControlEvents:UIControlEventTouchDown]; 
    img.titleLabel.textColor=[UIColor blackColor]; 
    text=[[UITextField alloc]initWithFrame:CGRectMake(105, 30, 200, 30)]; 
    [email protected]"Name"; 
    [text setBorderStyle:UITextBorderStyleRoundedRect]; 
    [cell addSubview:text]; 
    [cell addSubview:img]; 
     } 
     } 

      cell.textLabel.text = @"NameOFData "; 

     return cell; 
    } 
+0

对不起。没有同样的错误看到我更新的代码 – 2013-03-14 12:47:33

+0

把你的整个组件(imgaeView,textfield,.... etch)in ... if(cell == nil) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; } ... – iPatel 2013-03-14 12:48:51

+0

@ IOS234-check my edited ..;) – iPatel 2013-03-14 12:51:39