【问题标题】:Changing tableview cells items更改表格视图单元格项目
【发布时间】:2011-10-22 07:47:45
【问题描述】:

我有一个包含 4 个可变数组的应用程序,我已经在 tableview 单元格中显示了一个项目数组,但是我想在按钮单击时更改表格单元格的内容,我定义了 4 个按钮,我需要的是当我单击时一个按钮,tableview 单元格更改数组并显示相应的数组项。我可以在按钮单击中重用 tableview 单元格,下面是我的 tableview 单元格代码。

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [_titleArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Each subview in the cell will be identified by a unique tag.
    static NSUInteger const kcatogeryLabelTag = 2;
    static NSUInteger const ktitleLabelTag = 3;
    static NSUInteger const kshortDiscriptionLabelTag = 4;
    static NSUInteger const knewsImageTag = 5;

    // Declare references to the subviews which will display the earthquake data.
    UILabel *catogeryLabel = nil;
    UILabel *titleLabel = nil;
    UILabel *shortDiscriptionLabel = nil;
    UIImageView *newsImage = nil;

    static NSString *kEarthquakeCellID = @"EarthquakeCellID";    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kEarthquakeCellID];
    if (cell == nil) {
        // No reusable cell was available, so we create a new cell and configure its subviews.
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:kEarthquakeCellID] autorelease];

        catogeryLabel = [[[UILabel alloc] initWithFrame:CGRectMake(91, 3, 190, 20)] autorelease];
        catogeryLabel.tag = kcatogeryLabelTag;
        catogeryLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:20];
        [cell.contentView addSubview:catogeryLabel];

        titleLabel = [[[UILabel alloc] initWithFrame:CGRectMake(91, 28, 170, 14)] autorelease];
        titleLabel.tag = ktitleLabelTag;
        titleLabel.font = [UIFont fontWithName:@"Manorama" size:10];
        [cell.contentView addSubview:titleLabel];

        shortDiscriptionLabel = [[[UILabel alloc] initWithFrame:CGRectMake(91, 50, 500, 20)] autorelease];
        shortDiscriptionLabel.tag = kshortDiscriptionLabelTag;
        shortDiscriptionLabel.font = [UIFont fontWithName:@"Manorama" size:14];
        shortDiscriptionLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
        [cell.contentView addSubview:shortDiscriptionLabel];

        newsImage = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loadingimage.jpg"]] autorelease];
        CGRect imageFrame =newsImage.frame;
        imageFrame.origin = CGPointMake(62, 2);
        newsImage.frame = imageFrame;
        newsImage.tag = knewsImageTag;
        newsImage.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
        [cell.contentView addSubview:newsImage];
    } else {
        // A reusable cell was available, so we just need to get a reference to the subviews
        // using their tags.
        //
        catogeryLabel = (UILabel *)[cell.contentView viewWithTag:kcatogeryLabelTag];
        titleLabel = (UILabel *)[cell.contentView viewWithTag:ktitleLabelTag];
        shortDiscriptionLabel = (UILabel *)[cell.contentView viewWithTag:kshortDiscriptionLabelTag];
        newsImage = (UIImageView *)[cell.contentView viewWithTag:knewsImageTag];
    }


    // Get the specific earthquake for this row.
    Row *newsSubcatogery = [_titleArray objectAtIndex:indexPath.row];

     // Set the relevant data for each subview in the cell.
      catogeryLabel.text = newsSubcatogery.categoryName; 

      titleLabel.text =  newsSubcatogery.title;

      shortDiscriptionLabel.text =   newsSubcatogery.shortDescription;

      NSURL *url = [NSURL  URLWithString:newsSubcatogery.thumbImage];
      NSData *data = [NSData dataWithContentsOfURL:url];
      UIImage *image = [[UIImage alloc] initWithData:data]; 
     // CGSize imageSize = [image size];
      [newsImage setImage:image];
      [image release], image = nil;

     return cell;
}

【问题讨论】:

  • @Skomski 这个问题有什么解决办法吗?谢谢

标签: objective-c ipad


【解决方案1】:

您必须在单击按钮时分配表格视图的tag 属性。

- (IBAction) btnClick1:(id)sender
{
   tableview.tag = 200;
   [tableview reload];
}

现在在所有表格视图的委托方法中检查表格视图的tag,例如

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    if([tableView tag] == 200)
    {
        return [otherArray count];
    }
    else if ([tableView tag] == 300)
    {
        return [anotherArray count];
    }
}

上面是示例代码,您必须相应地对其进行修改。希望它可以帮助你。

【讨论】:

  • 但是我在uiview中只有一个tableview。我想通过单击按钮在单个tableview中加载不同的数据,您的代码是否足以满足我的要求?
  • 你需要在不同的按钮点击上分配不同的表格视图标签,比如 btn1 然后 table.tag=200 , btn2 然后 table.tag=300 明智的..
  • _titleArray 包含解析的 xml 对象,我还有三个这样的解析数组,tableview 单元格中的标签和图像的值是否与按钮单击相对应?
  • 您的单元格是否包含相同数量的标签、图像等?
  • 它现在对我来说工作得很好,但是如果单元格包含不同数量的标签和图像,如何编码?