更改tableview单元格项目

时间:2011-08-08 11:28:19

标签: objective-c ipad

我有一个包含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;
}

1 个答案:

答案 0 :(得分:0)

您必须在按钮点击时分配表格视图的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];
    }
}

上面是您必须相应修改它的示例代码。希望它能帮到你。