【发布时间】: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